« ソースコードをカラーHTML化する | メイン | grep、awk、find メモ »

ethna

1つのシステムで見栄えを切り替える

ethna の action・view は共通でテンプレートだけを切り替える。
shopA は
APPID/template/ja/shopA/Index.tpl
shopB は
APPID/template/ja/shopB/Index.tpl
さらに、shopXを追加したときにテンプレートが無ければ自動的に標準を使う。
APPID/template/ja/standard/Index.tpl
次のように実現する(実験的アプローチ)
class APPID_Controller extends Ethna_Controller に
    var $switchBase = 'standard'; ///    template/ja/standard/~ 「standard」は、他の切替と重複しない前提
/// var $switchBase = ''; /// template/ja/~    使わない時は空白に
    var $switchName = ''; /// template/ja/★/~
    
    function getTemplatedir(){
        $template    = parent::getTemplatedir();
        /// ↓さらに追加する
        if( 0 < strlen( $this->switchBase ) ){
            /// switchBase 制御するとき
            if( 0 < strlen( $this->switchName ) ){
                /// switchName 切替するとき
                $template .= '/' . $this->switchName;
            }else{
                /// switchName 切替なければswitchBaseデフォルト動作
                $template .= '/' . $this->switchBase;
            }
        }/// switchBase 制御なければ通常の動作
        return $template;
    }
    
    function setSwitchName($switchName){
        $this->switchName = $switchName;
    }
    
    function getSwitchBase(){
        return $this->switchBase;
    }
を追加。
class APPID_ViewClass extends Ethna_ViewClass に
    function forward(){
        $controller    =& $this->backend->getController();
        if( 0 < strlen( $controller->getSwitchBase() ) ){
            /// switchBase 制御するとき
            $Templatedir    = $controller->getTemplatedir();
            if( ! file_exists( "{$Templatedir}/{$this->forward_path}" ) ){
                /// テンプレートファイルがなければデフォルト(基底)にもどす
                $controller->setSwitchName( '' );
            }
        }/// switchBase 制御なければ通常の動作
        /// ↑先に切り替える
        parent::forward();
    }
を追加。
ethna add-template ProductList
コマンドで基底(標準)のディレクトリに
APPID/template/ja/standard/ProductList.tpl
テンプレートが生成。必要なshop~だけコピーしてカスタマイズ。

あとは、エントリーポイント(入り口)やログイン属性によって action・view などで切替。
shopA のテンプレートを使うときは、
$controller =& $this->backend->getController();
$controller->setSwitchName( 'shopA' );
shopX のテンプレートを使うときは、
$controller->setSwitchName( 'shopX' );
切替先のテンプレートが無ければ、基底の standard/~ を採用される。

Ethna-2.3.7、Ethna-2.5.0-preview3、Ethna-2.5.0-preview5 で確認。
ただし、Ethna-2.5.0-preview5 で利用する場合
APPID/template/ja/standard/layout.tpl
APPID/template/ja/standard/error403.tpl
APPID/template/ja/standard/error404.tpl
APPID/template/ja/standard/error500.tpl
の継承を考慮していないので各切替先に同じものが必要。

注意:Ethnaではクラスファクトリで1つのレンダラーオブジェクトを使いまわすのでEthna_MailSender などを使った後、このgetSwitchName()してもTemplatedirが更新できないので注意が必要。その2に続く・・・

トラックバック

このエントリーのトラックバックURL:
https://www.remix.asia/cgi/mt/mt-tb.cgi/6588

コメントを投稿

(いままで、ここでコメントしたことがないときは、コメントを表示する前にこのブログのオーナーの承認が必要になることがあります。承認されるまではコメントは表示されません。そのときはしばらく待ってください。)