Yaf_Controller_Abstract::render
(Since Yaf 1.0.0.5)
public Yaf_Response_Abstract Yaf_Controller_Abstract::render( string $action ,
array $tpl_vars
= NULL );
渲染视图模板, 得到渲染结果
![]() |
注意 |
|---|---|
| 此方法是对Yaf_View_Interface::render的包装 |
例 11.53. Yaf_Controller_Abstract::render 的例子
<?php
class IndexController extends Yaf_Controller_Abstract {
public funciton init() {
/* 首先关闭自动渲染 */
Yaf_Dispatcher::getInstance()->disableView();
}
public function indexAction() {
$this->initView();
/* 自己输出响应 */
echo $this->render("test.phtml");
}
}
?>
render只返回渲染结果, 并不会自动把结果写入响应. 如果需要对渲染结果做后处理, 可以先关闭自动渲染, 再用Yaf_Response_Abstract::setBody将结果设置为响应主体:
例 11.94. Yaf_Controller_Abstract::render 后处理渲染结果的例子
<?php
class ProductController extends Yaf_Controller_Abstract {
public function indexAction() {
// 不自动渲染默认模板
Yaf_Dispatcher::getInstance()->disableView();
// 渲染product/index.phtml并返回输出,
// 而不是像display那样直接发送
$html = $this->render("product/index.phtml", array(
"products" => array("yaf", "php"),
));
// 后处理后设置为响应主体
$this->getResponse()->setBody($html);
}
}
?>