php: 2008年8月アーカイブ

Zend FrameWorkを使用してみる事にしたがテンプレートエンジンはSmartyを使いたい
そこで調べてみると出来るみたい
このサイト参考にチャレンジ開始
http://wadslab.net/2008/03/zend_view_smarty/
・Smartyラッパーの作成
  application/lib/ViewSmarty.phpを作成
  内容は参考サイトより



require_once 'Zend/View/Interface.php';
require_once 'Smarty/Smarty.class.php';

class 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;
}
}




・templates,template_cの作成
  application/templates

  application/template_c




・ViewRendererアクションヘルパーに組み込まずに使用


$this->_helper->viewRenderer->setNoRender(true);
$tmplete_dir = "path";
$opt = array(
 "compile_dir" => "path"
);
$view = new ViewSmarty($tmplete_dir, $opt);
$view->assign("bar", "bar");
$rendered = $view->render('index.tpl');
print $rendered



・ViewSmartyクラスをViewRendererアクションヘルパーに組み込みは以下のエラー理由が分からず断念


Warning: Smarty error: unable to read resource: "index.tpl" in Smarty.class.php on line 1092


Quote!(引用する!) はてなブックマークに追加

アルバム

Twitter

このアーカイブについて

このページには、2008年8月以降に書かれたブログ記事のうちphpカテゴリに属しているものが含まれています。

前のアーカイブはphp: 2008年7月です。

次のアーカイブはphp: 2008年11月です。

最近のコンテンツはインデックスページで見られます。過去に書かれたものはアーカイブのページで見られます。