基于上一篇所讲的多模块结构进行。
1.下载Smarty,然后复制到library目录下
2.通过插件来控制Smarty模板所在目录,具体的结构如下:
整合Smarty时自定义插件目录结构
3. 通过插件来控制目录
App->controller->plugin->smarty.php :
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.项目入口文件对项目进行设置和启动:
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里面添加:
1 | public function indexAction() |
3 | $this ->smarty->assign( 'index_content' , 'asdfasdfasdf' ); |
4 | $this ->smarty->display( 'index/index.phtml' ); |
在Default/Views/Scripts/Index/index.phtml里面添加:
1 | IndexAction传递过来的内容:{{ $index_content }} |
运行成功,说明没有问题,可以成功显示传过来的值。