### 模板
The Yaf_View_Simple class
官方文檔:http://www.laruence.com/manual/yaf.class.view.html
Yaf_View_Simple是Yaf自帶的視圖引擎, 它追求性能, 所以并沒有提供類似Smarty那樣的多樣功能, 和復雜的語法.
對于Yaf_View_Simple的視圖模板, 就是普通的PHP腳本, 對于通過Yaf_View_Interface::assgin的模板變量, 可在視圖模板中直接通過變量名使用.
#### 使用$this->getView()->assign()在控制器中定義變量
`<?php
class IndexController extends Yaf_Controller_Abstract {
public function indexAction() {
$mod = new UserModel();
$list = $mod->where('id',1)->get();
$this->getView()->assign("list", $list);
$this->getView()->assign("title", "Smarty Hello World");
$this->getView()->assign("content", "Hello World");
}`
#### 在模板文件中使用php腳本來輸出
`<html>
<head>
<meta charset="utf-8">
<title><?php echo $title;?></title>
</head>
<body>
<?php echo $content;?>
<?php foreach($list as $val){?>
<p><?php echo $val['username']?></p>
<?}?>
</body>
</html>`
### 關閉Yaf框架里的自動加載模板
Yaf框架默認是開啟自動加載模板的,如要關閉自動加載,可在Bootstrap.php里設置全局關閉,如:
`<?php
class Bootstrap extends Yaf_Bootstrap_Abstract
{
public function _initConfig()
{
Yaf_Registry::set('config', Yaf_Application::app()->getConfig());
Yaf_Dispatcher::getInstance()->autoRender(FALSE); // 關閉自動加載模板
}
}`
單獨關閉模板加載,可以需要關閉的控制器內利用Yaf_Dispatcher::getInstance()->disableView()操作:
`<?php
class IndexController extends Yaf_Controller_Abstract {
/**
* Controller的init方法會被自動首先調用
*/
public function init() {
/**
* 如果是Ajax請求, 則關閉HTML輸出
*/
if ($this->getRequest()->isXmlHttpRequest()) {
Yaf_Dispatcher::getInstance()->disableView();
}
}
}
?>`
### 手動調用指定模板
在控制器里手動調用的方式有2種:
一,調用當前$this->_module目錄下的模版,下面是手動調用view/index/目錄下hello.phtml模板
`<?php
class IndexController extends Yaf_Controller_Abstract
{
public function indexAction()
{
$this->getView()->assign("content", "Hello World");
$this->display('hello');
}
}`
二,隨意調用view目錄下的模板,下面是調用view/test/world.phtml模板
`<?php
class IndexController extends Yaf_Controller_Abstract
{
public function indexAction()
{
$this->getView()->assign("content", "Hello World");
$this->getView()->display('test/world.phtml');
}
}`