Wadslog

[Zend Framework][Smarty] Zend_ControllerへSmarty組み込む

Mar 17, 2008

Zend_ViewでSmartyを使う方法は、このページを参考に、Zend_View_Interfaceを実装して、最低限Smartyのプロパティである$template_dirと$compile_dirを設定してあげれば使えます。``` require_once ‘Zend/View/Interface.php’; require_once ‘Smarty/Smarty.class.php’;

class ViewSmarty implements Zend_View_Interface { … } 上記のようなviewクラスを作ったら、以下のように初期化します。 // プロパティの設定 $templete_dir = “path/to/tmplete/dir”; $opt = array( “compile_dir”=>“path/to/compile/dir” ); $view = new ViewSmarty($tmplete_dir, $opt);

// 以下のような設定も可能 //$opt = array( // “templete_dir” => “path/to/tmplete/dir”, // “compile_dir” => “path/to/compile/dir” //); //$view = new ViewSmarty(null, $opt); 以下のように変数をアサインして画面を描画します。 $view->foo = “foo”; $view->assign(“bar”, “bar”); $view->render(“hello.phtml”); ViewSmartyクラスをViewRendererアクションヘルパーに組み込む法方は以下のとおりです。 $viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer(new My_View_Smarty(null, $option)); Zend_Controller_Action_HelperBroker::addHelper($viewRenderer); これでviewRendererでSmartyを使えるようになりました。 **2008/4/2追記** Zend Framework 1.5 でリファレンスマニュアルのSmartyラッパーの例をそのまま使うと、エラーが出る場合があるので[こちら](http://wadslab.net/2008/04/zend_view_smarty-4/)もご覧ください。 私は以下のようなviewを使っています。 require_once ‘Zend/View/Interface.php’; require_once ‘Smarty/Smarty.class.php’;

class Lib_ViewSmarty implements Zend_View_Interface { protected $_smarty;

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;
    }
    
    $this->\_smarty->default\_modifiers=array('sanitize');
}

public function getEngine()
{
    return $this->\_smarty;
}

public function setScriptPath($path)
{
    if (is\_readable($path)) {
        $this->\_smarty->template\_dir = $path;
        return;
    }

    throw new Exception('無効なパスが指定されました:'.$path);
}

public function getScriptPaths()
{
    return array($this->\_smarty->template\_dir);
}

public function setBasePath($path, $prefix = 'Zend\_View')
{
    return $this->setScriptPath($path);
}

public function addBasePath($path, $prefix = 'Zend\_View')
{
    return $this->setScriptPath($path);
}

public function \_\_set($key, $val)
{
    $this->\_smarty->assign($key, $val);
}

public function \_\_get($key)
{
    return $this->\_smarty->get\_template\_vars($key);
}

public function \_\_isset($key)
{
    return (null !== $this->\_smarty->get\_template\_vars($key));
}

public function \_\_unset($key)
{
    $this->\_smarty->clear\_assign($key);
}

public function assign($spec, $value = null)
{
    if (is\_array($spec)) {
        $this->\_smarty->assign($spec);
        return;
    }

    $this->\_smarty->assign($spec, $value);
}

public function clearVars()
{
    $this->\_smarty->clear\_all\_assign();
}

public function render($name)
{
    return $this->\_smarty->fetch($name);
}

public function getVars()
{
    $vars = $this->\_smarty->get\_template\_vars();
    unset($vars\["SCRIPT\_NAME"\]);
    return $vars;
}

}

comments powered by Disqus