Smartyを利用する方法

基本的にはZend Frameworkのドキュメントに書かれているものと同じです。
共通テンプレートのZend_Layoutを使用するための記述は、文字色で書いています。

またモジュールを分けている場合には、Smartyのcompile_idなどを使ってコンパイルファイルを変更する必要があります。
対策をしていないと別のモジュールに同じコントローラ・アクションがあった場合には、そっちのテンプレートコンパイルファイルを読んだりします。

ディレクトリ構成

application
│├ configs
││└ application.ini
│├ views
││└ scripts
││ └ index
││  └ index.tpl
│├ layouts
││└ scripts
││ └ layout.tpl
│└ Bootstrap.php
├ data
│└ smarty
│ ├ cache
│ └ templates_clibrary
 ├ Custom
 │ └ Smarty.php
 └ Smarty
  ├ libs
  │├ Smarty.class.php
  │├ ……
  ├ ……

ソースコード

application/configs/application.ini

resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
resources.layout.viewSuffix = "tpl"

view.smarty.cache_dir = APPLICATION_PATH "/../data/smarty/cache/"
view.smarty.compile_dir = APPLICATION_PATH "/../data/smarty/templates_c/"
view.scriptPath = APPLICATION_PATH "/views/scripts"

application/Bootstrap.php

<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    protected function _initView()
    {
        $options = new Zend_Config($this->getOptions());
        $view_config = $options->view->toArray();
        $smarty_config = $options->view->smarty->toArray();

        require_once 'Custom/Smarty.php';
        $view = new Custom_View_Smarty($view_config['scriptPath'],$smarty_config);
        $viewRenderer =
            Zend_Controller_Action_HelperBroker::getStaticHelper("ViewRenderer");
        $viewRenderer->setView($view)
                     ->setViewBasePathSpec($view->_smarty->template_dir)
                     ->setViewScriptPathSpec(':controller/:action.:suffix')
                     ->setViewScriptPathNoControllerSpec(':action.:suffix')
                     ->setViewSuffix('tpl');
    }
}

library/Custom/Smarty.php

<?php

require_once 'Smarty/libs/Smarty.class.php';

class Custom_View_Smarty implements Zend_View_Interface
{
    /**
     * Smarty object
     * @var Smarty
     */
    public $_smarty;

    /**
     * コンストラクタ
     *
     * @param string $tmplPath
     * @param array $extraParams
     * @return void
     */
    public function __construct($tmplPath = null, $extraParams = array())
    {
        $this->_smarty = new Smarty;
        if (null !== $tmplPath) {
            $this->setScriptPath($tmplPath);
        }
        foreach ($extraParams as $key => $value) {
            $this->_smarty->$key = $value;
        }
    }

    /**
     * テンプレートエンジンオブジェクトを返します
     *
     * @return Smarty
     */
    public function getEngine()
    {
        return $this->_smarty;
    }

    /**
     * テンプレートへのパスを設定します
     *
     * @param string $path パスとして設定するディレクトリ
     * @return void
     */
    public function setScriptPath($path)
    {
        if (is_readable($path)) {
            $this->_smarty->template_dir = $path;
            return;
        }
        throw new Exception('無効なパスが指定されました');
    }

    /**
     * 現在のテンプレートディレクトリを取得します
     *
     * @return string
     */
    public function getScriptPaths()
    {
        return array($this->_smarty->template_dir);
    }

    /**
     * setScriptPath へのエイリアス
     *
     * @param string $path
     * @param string $prefix Unused
     * @return void
     */
    public function setBasePath($path, $prefix = 'Zend_View')
    {
        return $this->setScriptPath($path);
    }

    /**
     * setScriptPath へのエイリアス
     *
     * @param string $path
     * @param string $prefix Unused
     * @return void
     */
    public function addBasePath($path, $prefix = 'Zend_View')
    {
        return $this->setScriptPath($path);
    }

    /**
     * 変数をテンプレートに代入します
     *
     * @param string $key 変数名
     * @param mixed $val 変数の値
     * @return void
     */
    public function __set($key, $val)
    {
        if(is_object($key)){
            return;
        }
        $this->_smarty->assign($key, $val);
    }

    /**
     * empty() や isset() のテストが動作するようにします
     *
     * @param string $key
     * @return boolean
     */
    public function __isset($key)
    {
        return (null !== $this->_smarty->get_template_vars($key));
    }

    /**
     * オブジェクトのプロパティに対して unset() が動作するようにします
     *
     * @param string $key
     * @return void
     */
    public function __unset($key)
    {
        $this->_smarty->clear_assign($key);
    }

    /**
     * 変数をテンプレートに代入します
     *
     * 指定したキーを指定した値に設定します。あるいは、
     * キー => 値 形式の配列で一括設定します
     *
     * @see __set()
     * @param string|array $spec 使用する代入方式 (キー、あるいは キー => 値 の配列)
     * @param mixed $value (オプション) 名前を指定して代入する場合は、ここで値を指定します
     * @return void
     */
    public function assign($spec, $value = null)
    {
        if (is_array($spec)) {
            $this->_smarty->assign($spec);
            return;
        }
        $this->_smarty->assign($spec, $value);
    }

    /**
     * 代入済みのすべての変数を削除します
     *
     * Zend_View に {@link assign()} やプロパティ
     * ({@link __get()}/{@link __set()}) で代入された変数をすべて削除します
     *
     * @return void
     */
    public function clearVars()
    {
        $this->_smarty->clear_all_assign();
    }

    /**
     * テンプレートを処理し、結果を出力します
     *
     * @param string $name 処理するテンプレート
     * @return string 出力結果
     */
    public function render($name)
    {
        $this->_smarty->assign('layout',Zend_Layout::getMvcInstance());
        return $this->_smarty->fetch($name);
    }

    /**
     * Zend_Layoutを使用します。
     *
     * @return object Zend_Layoutインスタンス
     */
    public function layout()
    {
        return Zend_Layout::getMvcInstance();
    }
}

application/layouts/scripts/layout.tpl

{$layout->content|smarty:nodefaults}

関連記事

スポンサーリンク

gitmodules

ホームページ製作・web系アプリ系の製作案件募集中です。

上に戻る