Php/zend framework/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 <h2>{{$message|escape:html}}

  {{if 'development' == $smarty.const.APPLICATION_ENV}}

<h3>Exception information:

  <p>
      <b>Message: {{$exception->getMessage()}}

  <p>
      <b>Class: {{$exception|get_class}}

  <p>
      <b>Code: {{$exception->getCode()}}

  <p>
      <b>File: {{$exception->getFile()}}({{$exception->getLine()}})

<h3>Stack trace:

  <pre>{{$exception->getTraceAsString()}}

<h3>Request Parameters:

  <pre>
  {{foreach item=item key=key from=$request->getParams()}}
  {{$key}} => {{$item}}

{{/foreach}}

  {{/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();

Fatal error: Smarty error: unable to write toエラーが出る場合

以下表示が出て、compilesのdirに書き込みでない場合は、compilesの権限を777にしてみる。

Fatal error: Smarty error: unable to write to

selinuxが有効になってる可能性があるので確認する

$ getenforce
Enforcing

有効(Enforcing)になってる場合は、一旦無効にしてみる。

$ sudo setenforce 0

Vps/kagoya [ショートカット]