修改Action中Smarty模版
$this->_helper->viewRenderer->setNoRender();$cfg = new Zend_Config_Ini( APPLICATION_PATH . ‘/configs/view.ini’, ‘Smarty’ );$viewCfg = $cfg->toArray();$view = new Zamp_View_Smarty( $viewCfg );$view->assign( ‘username’, ‘baobao’ );$view->setScriptPath( $viewCfg[‘template_dir’] );echo $view->render( ‘smarty/hello2.phtml’ );
修改Controller中Smarty模版
创建initView函数,内容如下:
public function initView(){$cfgObj = new Zend_Config_Ini( APPLICATION_PATH . ‘/configs/view.ini’, ‘Smarty’ );$smarty_cfg = $cfgObj->toArray();$modDir = $this->_request->getModuleName();$dirs = $this->getFrontController()->getControllerDirectory();if ( empty( $modDir ) || !isset( $modDir ) ){$modDir = $this->getFrontController()->getDefaultModule();}$baseDir = dirname( $dirs[$modDir] ) . DIRECTORY_SEPARATOR . ‘views’ . DIRECTORY_SEPARATOR . ‘scripts’ . DIRECTORY_SEPARATOR;if (!file_exists($baseDir) || !is_dir($baseDir)) {require_once ‘Zend/Controller/Exception.php’;throw new Zend_Controller_Exception(‘Missing base view directory (“‘ . $baseDir . ‘”)’);}$this->view = new Zamp_View_Smarty( $smarty_cfg );$this->view->setScriptPath( $baseDir );$this->_helper->viewRenderer->setView( $this->view );return $this->view;}
创建init函数,并在期内增加initView函数,如下:
public function init(){$this->initView();}
注意:此应用中必须设置viewRender的View,因为Controller应用中render是调用viewRender中的render函数。而viewRender中默认的是启动自身的应用而非使用Zend_View中的render应用。
在Bootstrap中Smarty模版
在Bootstrap引导文件中创建_initView函数。
protected function _initView(){$cfgObj = new Zend_Config_Ini( APPLICATION_PATH . ‘/configs/view.ini’, ‘Smarty’ );$smarty_cfg = $cfgObj->toArray();$view = new Zamp_View_Smarty( $smarty_cfg );$view->setScriptPath( $smarty_cfg[‘template_dir’] );$renderer = new Zend_Controller_Action_Helper_ViewRenderer();$renderer->setView( $view );$renderer->setViewBasePathSpec( $smarty_cfg[‘template_dir’] );Zend_Controller_Action_HelperBroker::addHelper( $renderer );}
注意:在此应用中$renderer->setViewBasePathSpec( $smarty_cfg[‘template_dir’] );必须执行此函数,因为ViewRenderer类中执行的默认模版地址是有此函数执行的,而其他函数是不予执行的。
评论