基於上一篇所講的多模塊結構進行。
1.下載Smarty,而後複製到library目錄下
2.經過插件來控制Smarty模板所在目錄,具體的結構以下:php
整合Smarty時自定義插件目錄結構html
3. 經過插件來控制目錄
App->controller->plugin->smarty.php :app
1 |
class App_Controller_Plugin_Smarty extends Zend_Controller_Plugin_Abstract |
3 |
public function preDispatch(Zend_Controller_Request_Abstract $request ) |
5 |
$module = $request ->module; //獲得當前模塊 |
6 |
$view = Zend_Registry::get( 'smarty' ); |
7 |
$view ->setBasePath(APPLICATION_PATH. '/modules/' . $module ); //指定當前模塊目錄 |
App->View->Smarty.php :測試
01 |
class App_View_Smarty extends Smarty |
03 |
public function __construct( $extraParams = array ()) |
06 |
foreach ( $extraParams as $key => $value ){ |
11 |
public function setBasePath( $path ) |
13 |
$path = rtrim( $path , '/\\' ) . DIRECTORY_SEPARATOR; |
14 |
$this ->template_dir = $path . 'views/scripts' ; |
15 |
$this ->compile_dir = $path . 'views/template_c' ; |
4.修改配置文件,添加Smarty設置:
[smarty]
left_delimiter=」{{」
right_delimiter=」}}」
caching=0
5.項目入口文件對項目進行設置和啓動:ui
01 |
require_once 'Zend/Loader/Autoloader.php' ; //設置自動加載類 |
02 |
Zend_Loader_Autoloader::getInstance()->setFallbackAutoloader(TRUE); |
04 |
$config = new Zend_Config_Ini(APPLICATION_PATH. '/configs/application.ini' ,null, true); |
05 |
Zend_Registry::set( 'config' , $config ); |
07 |
$smarty = new App_View_Smarty( $config ->smarty->toarray()); |
08 |
Zend_Registry::set( 'smarty' , $smarty ); |
10 |
$frontController = Zend_Controller_Front::getInstance(); |
11 |
$frontController ->setControllerDirectory(APPLICATION_PATH. "/modules/default/controllers" , 'default' ) |
12 |
->setControllerDirectory(APPLICATION_PATH. "/modules/admin/controllers" , 'admin' ) |
13 |
->setModuleControllerDirectoryName( "controllers" ) |
14 |
->addModuleDirectory(APPLICATION_PATH. '/modules' ) |
15 |
->setDefaultModule( 'default' ) |
16 |
->setParam( 'noViewRenderer' , true) |
17 |
->setParam( 'noErrorHandler' , true) |
18 |
->setParam( 'useDefaultControllerAlways' , true) |
19 |
->registerPlugin( new App_Controller_Plugin_Smarty()) |
20 |
->throwExceptions(true) |
6.使用並測試成果:
在Default/Controllers/IndexController.php裏面添加:this
1 |
public function indexAction() |
3 |
$this ->smarty->assign( 'index_content' , 'asdfasdfasdf' ); |
4 |
$this ->smarty->display( 'index/index.phtml' ); |
在Default/Views/Scripts/Index/index.phtml裏面添加:spa
1 |
IndexAction傳遞過來的內容:{{ $index_content }} |
運行成功,說明沒有問題,能夠成功顯示傳過來的值。插件