Zend FrameworkとSmartyの連携:その5-コントローラの処理

■ 自動レンダリングする実装

Smarty変数 “title”, “message” に、値をセットするのみ。

<?php
class IndexController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}

public function indexAction()
{
$this->view->title = ‘Zend Framework Default Application’;
$this->view->message = ‘Smarty render 表示’;
}

}

■ 自動レンダリングしない実装

自動レンダリングと同様に、Smarty変数 “title”, “message” に、値をセットする。
postDispatch へ、レンダリング情報をセットする。
postDispatch 処理では、render結果を明示的に取得し、自動レンダリングを無効にしたあと、render結果を出力する。

ひとつのアクションで、処理により Smartyのテンプレートを切替えたい時に有用なのではと思う。

<?php
class IndexController extends Zend_Controller_Action
{
private $auto_render = true;
private $smarty_tpl = “”;

public function init()
{
/* Initialize action controller here */
}

public function postDispatch()
{
if ($this->auto_render == false) {
$html = $this->view->render($this->smarty_tpl);
$this->_helper->viewRenderer->setNoRender();
$this->getResponse()->setBody($html);
}
}

public function indexAction()
{
$this->view->title = ‘Zend Framework Default Application’;
$this->view->message = ‘Smarty render 表示’;

$this->auto_render = false;
$this->smarty_tpl = ‘index/index.tpl’;
}

}

関連リンク

コメントを残す