Zend Framework 1.10.x 多模块整合smarty模板


http://www.zendchina.net/?action-viewnews-itemid-1937

最近在总有人问“ZF如何整合Smarty”,刚刚浏览网页的时候看到这篇帖子写的挺详细,特转载整理过来供大家学习参考!*注意:阅读时注意用颜色标记过的地方

application/configs/application.ini
配置文件如下
[production]
; Timezone
phpSettings.date.timezone = “Asia/Shanghai”
; Debug output
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0

; Include Paths
includePaths.library = APPLICATION_PATH “/../library”

;NameSpace
appnamespace = “Application”

;resources
autoloaderNamespaces[] = “MyApp_”
;resources.frontController.plugins.Whatever = “MyApp_Controller_Plugin_Whatever”

; Bootstrap
bootstrap.path = APPLICATION_PATH “/Bootstrap.php”
bootstrap.class = “Bootstrap”

; Front Controller
resources.frontController.moduleDirectory                       = APPLICATION_PATH “/modules/”
resources.frontController.moduleControllerDirectoryName         = “controllers”
resources.frontController.defaultModule                         = “default”
resources.modules[] =

; Views
resources.view.basePath = APPLICATION_PATH “/modules/default/views”
;resources.layout.layout = “layout”
;resources.layout.layoutPath = APPLICATION_PATH “/layouts”
resources.view[] =

; Database
resources.db.adapter = PDO_MYSQL
resources.db.params.host = localhost
resources.db.params.username = root
resources.db.params.password =
resources.db.params.dbname = test
resources.db.params.driver_options.1002 = “SET NAMES UTF8″

; session
resources.session.save_path = APPLICATION_PATH “/../data/session”
resources.session.use_only_cookies = true
resources.session.remember_me_seconds = 20

[staging : production]

[testing : production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0

[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1



最外层的引导

application/Bootstrap.php
<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {

//加载db

//这部分如果不是有特别的需要其实也没有必要写了
protected function _initDb() {

$resources = $this->getPluginResource ( ‘db’ );
$db = $resources->getDbAdapter ();
Zend_Db_Table::setDefaultAdapter ( $db );
Zend_Registry::set ( ‘db’, $db );

}
//加载smarty插件
public function _initView() {

Zend_Controller_Front::getInstance()->registerPlugin(new MyApp_Controller_Plugin_Smarty());

//或者改用在application.ini配置中增加一句配置,可以达到同样效果
//resources.frontController.plugins.smarty = MyApp_Controller_Plugin_Smarty
}
}
多模块引导继承Zend_Application_ Module_Bootstrap, 而不再是Zend_Application_ Bootstrap_Bootstrap
application/modules/admin/Bootstrap.php

<?php
class Admin_Bootstrap extends Zend_Application_Module_Bootstrap {

public $view;

protected function _initAutoLoad() {
$autoloader = Zend_Loader_Autoloader::getInstance ();
$autoloader->suppressNotFoundWarnings ( false );

$moduleLoader = new Zend_Application_Module_Autoloader (
array (
‘namespace’ => ‘Admin’,
‘basePath’ => APPLICATION_PATH . ‘\modules\admin’,
‘resourceTypes’ => array (
‘model’ => array (‘path’ => ‘model’, ‘namespace’ => ‘Model’ ),
‘dbtable’ => array (‘path’ => ‘models/DbTable’,
‘namespace’ => ‘Model_DbTable’ )
)
)

);
return $moduleLoader;
}

}

smarty接管view
smarty放在library目录下

library/Myapp/View/Smarty.php
<?php
require_once ‘Zend/View/Interface.php’;
require_once ‘Smarty/Smarty.class.php’;

//继续视图基类并实现视图接口,这样可使用视图助手
class MyApp_View_Smarty extends Zend_View_Abstract implements Zend_View_Interface
{
protected $_path;
protected $_engine;

public function __construct($template_dir,$compile_dir){

$this->_engine=new Smarty(new Zend_View());
$this->_engine->left_delimiter  = ‘<{‘;
$this->_engine->right_delimiter = ‘}>’;
$this->_engine->template_dir    = $template_dir;
$this->_engine->compile_dir     = $compile_dir.’template_c/’;

//转载注释:此处也可以用调用配置文件的方式实现Smarty的动态配置

}

public function getEngine(){
return $this->_engine;
}

public function __set($key,$val){
$this->_engine->assign($key,$val);
}

public function __get($key){
return $this->_engine->get_template_vars($key);
}

public function __isset($key){
return $this->_engine->get_template_vars($key) !== null;
}

public function __unset($key){
return $this->_engine->clear_assign($key);
}

public function assign($spec,$value=null){
if(is_array($spec)){
$this->_engine->assign($spec);
return;
}

$this->_engine->assign($spec,$value);
}

public function clearVars(){
$this->_engine->clear_all_assign();
}

public function render($name){
return $this->_engine->fetch(strtolower($name));
}

public function _run(){

}
}



smarty写为插件形式 并在根引导中初始化
library/Myapp/Controller/Plugin/Smarty.php

<?php
class MyApp_Controller_Plugin_Smarty extends Zend_Controller_Plugin_Abstract
{

//dispatchLoopStartup() 在 Zend_Controller_Front 进入其分发循环(dispatch loop)前被调用
public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
{
$module = $request->module;
$controller = $request->controller;
$templete_c = APPLICATION_PATH . ‘/modules/’.$module.’/views/scripts/’;
$compile_dir = APPLICATION_PATH . ‘/modules/’.$module.’/';

$vr=new Zend_Controller_Action_Helper_ViewRenderer();

$vr->setView(new MyApp_View_Smarty($templete_c,$compile_dir));
$vr->setViewSuffix(‘phtml’);
Zend_Controller_Action_HelperBroker::addHelper($vr);
}
}

模板写法:
———————————————————————
<{ if $username != “Guest” }>
<p id=”logged-in”>姓名:
<{ $user->username }>
<a href=”
———————这里是不能用url助手的,不知道原作者有没有测试过———————————————
<{ php }>
echo $this->url(array(
“controller”=>’index’,
‘action’=>’logout’
)
);
<{ /php }>
—————————end—————————————


“>Logout</a></p>

<{/if}>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值