facebook twitter hatena line email

Php/zend framework/smartyメモ

提供: 初心者エンジニアの簡易メモ
2016年2月17日 (水) 03:05時点におけるAdmin (トーク | 投稿記録)による版 (テンプレートにSmartyを使用)

移動: 案内検索

テンプレートにSmartyを使用

http://framework.zend.com/manual/ja/zend.view.scripts.html からZend_View_Smartyクラスをコピーし、Custom_View_Smartyに変更し、Custom/View/Smarty.phpへ保存。

上記はsmarty3ではエラーとなるので、以下を使用 php/zend_framework/smarty3用 [ショートカット]

index.php(フロントコントローラー)に以下を記載

require_once dirname(__FILE__) . '/../library/Custom/View/Smarty.php';
require_once 'Zend/Controller/Action/Helper/ViewRenderer.php';
require_once 'Zend/Config/Ini.php';
$setting = new Zend_Config_Ini('../application/configs/smarty.ini', 'SmartySetting');
$view = new Custom_View_Smarty(null, $setting);
$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
$viewRenderer->setView($view)
            ->setViewBasePathSpec("../application/views/templates")
            ->setViewScriptPathSpec(':controller/:action.:suffix')
            ->setViewScriptPathNoControllerSpec(':action.:suffix')
            ->setViewSuffix('tpl');

smarty.ini

[SmartySetting]
compile_dir=../application/views/templates_c
left_delimiter="{{"
right_delimiter="}}"

error.tplのサンプル

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>An error occurred</title>
</head>
<body>
  <h1>An error occurred</h1>
  <h2>{{$message|escape:html}}</h2>
  {{if 'development' == $smarty.const.APPLICATION_ENV}}
  <h3>Exception information:</h3>
  <p>
      <b>Message:</b> {{$exception->getMessage()}}
  </p>
  <p>
      <b>Class:</b> {{$exception|get_class}}
  </p>
  <p>
      <b>Code:</b> {{$exception->getCode()}}
  </p>
  <p>
      <b>File:</b> {{$exception->getFile()}}({{$exception->getLine()}})
  </p>
  <h3>Stack trace:</h3>
  <pre>{{$exception->getTraceAsString()}}
  </pre>
  <h3>Request Parameters:</h3>
  <pre>
  {{foreach item=item key=key from=$request->getParams()}}
  {{$key}} => {{$item}}
  {{/foreach}}</pre>
  {{/if}}
</body>
</html>

zend_applicationでmodulesを使用したときのサンプル

*bootstrap.phpへ追記
protected function _initSmarty()
{
    require_once dirname(__FILE__) . '/../library/Custom/View/Smarty.php';
    require_once 'Zend/Controller/Action/Helper/ViewRenderer.php';
    require_once 'Zend/Config/Ini.php';
    $request = new Zend_Controller_Request_Http();
    // module名がうまく取得できなかったので、無理やり
    preg_match("!^/([\w]*)/!", $request->getRequestUri(), $matches);
    $module = $matches[1];
    // ini追記モードで取得
    $setting = new Zend_Config_Ini('../application/configs/smarty.ini', 'SmartySetting', true);
    $setting->compile_dir = '../application/modules/' . $module . '/views/templates_c';
    $view = new Custom_View_Smarty(null, $setting);
    $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
    $viewRenderer->setView($view)
        ->setViewBasePathSpec('../application/modules/' . $module . '/views/templates')
        ->setViewScriptPathSpec(':controller/:action.:suffix')
        ->setViewScriptPathNoControllerSpec(':action.:suffix')
        ->setViewSuffix('tpl');
}

コントローラーからSmartyクラスを呼び出す

$smarty = $this->view->getEngine();