Zend Framework整合Smarty

基於上一篇所講的多模塊結構進行。
1.下載Smarty,而後複製到library目錄下
2.經過插件來控制Smarty模板所在目錄,具體的結構以下:php

自定義插件目錄結構

整合Smarty時自定義插件目錄結構html

3. 經過插件來控制目錄
App->controller->plugin->smarty.php  :app

1 class App_Controller_Plugin_Smarty extendsZend_Controller_Plugin_Abstract
2 {
3     public function preDispatch(Zend_Controller_Request_Abstract$request)
4     {
5         $module $request->module;  //獲得當前模塊
6         $view = Zend_Registry::get('smarty');
7         $view->setBasePath(APPLICATION_PATH.'/modules/'.$module); //指定當前模塊目錄
8     }
9 }

App->View->Smarty.php :測試

01 class App_View_Smarty extends Smarty
02 {
03     public function __construct($extraParams array())
04     {
05         $this->Smarty();
06         foreach ($extraParams as $key => $value){
07             $this->$key $value;
08         }
09     }
10  
11     public function setBasePath($path)
12     {
13         $path = rtrim($path'/\\') . DIRECTORY_SEPARATOR;
14         $this->template_dir = $path 'views/scripts';
15         $this->compile_dir  = $path 'views/template_c';
16     }
17 }

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);
03 //讀取配置文件信息
04 $config=newZend_Config_Ini(APPLICATION_PATH.'/configs/application.ini',null, true);
05 Zend_Registry::set('config',$config);
06 //調用Smarty插件,並進行設值
07 $smarty new App_View_Smarty($config->smarty->toarray());
08 Zend_Registry::set('smarty'$smarty);
09  
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)
21                     ->dispatch();

6.使用並測試成果:
在Default/Controllers/IndexController.php裏面添加:this

1 public function indexAction()
2 {
3 $this->smarty->assign('index_content','asdfasdfasdf');
4 $this->smarty->display('index/index.phtml');
5 }

在Default/Views/Scripts/Index/index.phtml裏面添加:spa

1 IndexAction傳遞過來的內容:{{$index_content}}

運行成功,說明沒有問題,能夠成功顯示傳過來的值。插件

相關文章
相關標籤/搜索