Zendframework 1.6整合Smarty
来源:互联网 发布:对java开发岗位的理解 编辑:程序博客网 时间:2024/06/07 01:31
参考文章: http://devzone.zend.com/node/view/id/120
部分中文翻译: http://www.codebit.cn/pub/html/php_mysql/tutorial/integrating_smarty_with_the_zend_framework/
通过 smarty 扩展zend framework view控制器的关键是集成 Zend_View_Abstract 类, 并从写某些方法, 然后用自己写的扩展类注册Zend_Controller_Action_Helper_ViewRenderer 视图控制器.
startboot, 部分代码如下,其中包括用自己写的类注册视图控制器, 至于Zend_Controller_Action_Helper_ViewRenderer, 只是一个视图引擎管理器, 连接视图应用,和底层视图引擎, 所以我们才可以改写中间层,也就是视图核心引擎, 该视图核心引擎默认是 Zend_View.
- <?php
- /**
- * Application base on Zend framework
- *
- * Note:
- * - The default template postfix is 'html'.
- * - The default view controller root directory is [MAIN_DIR].'/zendView/views'
- * - The default zend Framework package was install in the 'PHP/PEAR/zendFramework'
- * - Zend framework default does not checking POST,GET,COOKIE variable, So we should do it here.
- ***/
- /*
- * Global function define
- - [对象调试输出] print_obj(object);
- */
- function print_obj($o){
- echo "<p><pre>";
- var_dump($o);
- echo "</pre></p>";
- }
- /*
- * Basically define.
- */
- set_include_path(ini_get("include_path").";c:/webServer/php/PEAR/ZendFramework");
- define("MAIN_DIR",dirname(__FILE__));
- /*
- * include file
- - [前台控制器] Zend/Controller/Front.php
- - [基于Smarty的扩展类,与Zend view整合] Zend/Smarty/ZendSmarty.php
- - [助手经济人] Zend/Controller/Action/HelperBroker.php
- - [视图控制器] Zend/Controller/Action/Helpoer/ViewRenderer.php
- - [配置文件管理器] Zend/Config/Ini.php
- - [模块配置文件] [MAIN_DIR]/zendConfig/module.config.php
- - [Smarty模板配置参数] [MAIN_DIR]/zendConfig/base.config.php
- */
- require_once 'Zend/Controller/Front.php';
- require_once("Zend/Smarty/ZendSmarty.php");
- require_once("Zend/Controller/Action/HelperBroker.php");
- require_once("Zend/Controller/Action/Helper/ViewRenderer.php");
- require_once("Zend/Config/Ini.php");
- require_once("Totuba/PageStructureController.php");
- $confModule = include_once(MAIN_DIR."/zendConfig/module.config.php");
- $confGlobal = include_once(MAIN_DIR."/zendConfig/base.config.php");
- $confContentRegion = include_once(MAIN_DIR."/zendConfig/content_region.config.php");
- $confPageStructureMode = include_once(MAIN_DIR."/zendConfig/page_structure_mode.config.php");
- /*
- * Program section
- */
- $oFront = Zend_Controller_Front::getInstance();
- //--- Set module path configration ---
- $oFront->setControllerDirectory($confModule);
- //--- Zend framework view floor defination ---
- $aViewRendererOptions = array(
- "viewBasePathSpec"=>MAIN_DIR."/zendView/views/:module",
- "viewSuffix"=>"html"
- //"viewScriptPathSpec"=>":moduleDir/views/scripts/:controller/:action.:suffix",
- //"viewScriptPathNoControllerSpec"=>"default/views/scripts/:controller/:action.:suffix",
- //"viewSuffix"=>"phtml"
- );
- $aPageStructureControllerOptions = array(
- "contentRegionConfigration"=>$confContentRegion,
- "pageStructureModeConfigration"=>$confPageStructureMode,
- "applicationDirectory"=>MAIN_DIR."/application"
- );
- $oViewRender = new Zend_Controller_Action_Helper_ViewRenderer(new Zend_Smarty_ZendSmarty(array(),$confGlobal['smarty']),$aViewRendererOptions);
- $oPageStructureController = new Totuba_PageStructureController($oViewRender,$aPageStructureControllerOptions);
- Zend_Controller_Action_HelperBroker::addHelper($oViewRender);
- Zend_Controller_Action_HelperBroker::addHelper($oPageStructureController);
- //Totuba_PageStructureController::setOptions($aPageStructureControllerOptions);
- //Totuba_PageStructureController::testOptions();
- //--- Zend framework view floor defination [end] --- */
- $oRequest = $oFront->getRequest();
- //--- Before despatch, deleting unused global variables ---
- unset($confModule,$confGlobal);
- $oFront->dispatch();
用smarty 扩展的 Zend_View 原程序如下:
- <?php
- /*
- smarty implement Zend_View_Interface
- File name: ZendSmarty.php
- Date:2008-9-3
- Author: Jason Guan
- Email: Guan.xingmin@hotmail.com
- 这个类继承自Zend_View_Abstract 类,父类设计是按照Zend_View要求来设计的,这个不符合Smarty的运行环境和要求,
- 因此,在该类中,要重写部分父类的方法,支持Smarty,并且扩展该类,使其可以灵活支持模板管理,和路径准确定位。
- 对该类的调用是通过Zend_Controller_Action_Helper_ViewRenderer类来实现的,系统真正对模板或视图层的操作是通过
- 这个类来实现的,因此,对Zend_Controller_Action_Helper_ViewRenderer类也有一定扩展功能的必要使其可以完全支持
- Smarty和内部系统对视图层操作的要求。
- 对视图层的操作默认情况是 module/controller/view/scripts/controller/action.suffix,
- 但是应该可以通过指定模块控制器和行为,自由调用任何一个模块的视图。同时在调用任何视图的同时应该可以执行相应的
- 方法,也就是模块方法,为视图模板提供数据,这个也是系统内部模块自由调用的需求。
- */
- include_once("zend/Smarty/Smarty.class.php");
- include_once("zend/View/Abstract.php");
- include_once("Zend/Config/Ini.php");
- class Zend_Smarty_ZendSmarty extends Zend_View_Abstract{
- public $oSmarty = null ;
- //--- view base folder ---
- /*
- If the application view structure is base on module, the conf_baseDirectory is module/views
- Otherwise this conf_baseDirectory is "folder to application view"/
- */
- public $conf_baseDirectory = "";
- /**
- * Whether or not to use streams to mimic short tags
- * @var bool
- */
- private $_useViewStream = false;
- /**
- * Whether or not to use stream wrapper if short_open_tag is false
- * @var bool
- */
- private $_useStreamWrapper = false;
- /**
- * Constructor
- *
- * Register Zend_View_Stream stream wrapper if short tags are disabled.
- *
- * @param array $config
- * @param $smartyConf [Zend_Config_Ini:object]: the smarty configration data
- * @return void
- *
- */
- public function __construct($config = array(),$aSmartyConf)
- {
- $this->_useViewStream = (bool) ini_get('short_open_tag') ? false : true;
- if ($this->_useViewStream) {
- if (!in_array('zend.view', stream_get_wrappers())) {
- require_once 'Zend/View/Stream.php';
- stream_wrapper_register('zend.view', 'Zend_View_Stream');
- }
- }
- if (array_key_exists('useStreamWrapper', $config)) {
- $this->setUseStreamWrapper($config['useStreamWrapper']);
- }
- parent::__construct($config);
- $this->oSmarty = new Smarty();
- $this->setSmartyConfigration($aSmartyConf);
- }
- public function init(){
- }
- public function &getSmarty(){
- return $this->oSmarty ;
- }
- public function setSmartyConfigration($aConf){
- if(!is_array($aConf)){
- require_once 'Zend/View/Exception.php';
- throw new Zend_View_Exception('Setting smarty configration error,the parameter is not a valid Array!', $this);
- }
- foreach($aConf as $k=>$v){
- if(isset($this->oSmarty->$k) && ( "_" != substr($k,0,1))){
- $this->oSmarty->$k = $v ;
- }
- }
- return $this ;
- }
- /**
- * Directly assigns a variable to the view script.
- *
- * Checks first to ensure that the caller is not attempting to set a
- * protected or private member (by checking for a prefixed underscore); if
- * not, the public member is set; otherwise, an exception is raised.
- *
- * @param string $key The variable name.
- * @param mixed $val The variable value.
- * @return void
- * @throws Zend_View_Exception if an attempt to set a private or protected
- * member is detected
- *
- * This is a copy from Zend/View/Abstract.php
- */
- public function __set($key, $val)
- {
- if ('_' != substr($key, 0, 1)) {
- $this->$key = $val;
- return;
- }
- //--- This is the modibied rows ---
- $this->oSmarty->assign($key,$val);
- require_once 'Zend/View/Exception.php';
- throw new Zend_View_Exception('Setting private or protected class members is not allowed', $this);
- }
- /**
- * Allows unset() on object properties to work
- *
- * @param string $key
- * @return void
- */
- public function __unset($key)
- {
- if ('_' != substr($key, 0, 1) && isset($this->$key)) {
- unset($this->$key);
- }
- $this->oSmarty->clear_assign($key);
- }
- public function _setEscape($spec)
- {
- if(is_string($spec))
- //return parent::$this->_escape = $spec;
- return $this;
- }
- public function assign($spec, $value = null){
- if(is_string($spec)){
- $this->oSmarty->assign($spec,$value);
- }
- elseif(is_array($spec)){
- $this->oSmarty->assign($spec);
- }
- else{
- require_once 'Zend/View/Exception.php';
- throw new Zend_View_Exception('assign() expects a string or array, got '.gettype($var));
- }
- }
- public function getVars(){
- return $this->oSmarty->get_template_vars();
- }
- public function getVar($assignedVariableName){
- return $this->oSmarty->get_template_vars($assignedVariableName);
- }
- public function clearVars(){
- $this->oSmarty->clear_all_assign();
- }
- public function clearVar($assignedVariableName){
- $this->oSmarty->clear_assign($assignedVariableName);
- }
- /*
- This method is only used for Zend Framework internal view needing
- */
- public function render($name, $options=NULL){
- $outPutFile = $this->_script($name);
- $a = $this->oSmarty->fetch($outPutFile);
- return $this->filterOutPut( $a ); // filter output
- }
- /*
- Generally the tempPath is module/controller/action.html
- But it is allows to set other path
- */
- public function fetch($tempPath) {
- if($this->oSmarty->template_exists($tempPath)){
- return $this->oSmarty->fetch($tempPath);
- }else{
- require_once 'Zend/View/Exception.php';
- throw new Zend_View_Exception("For smarty, provided template is not exists [$tempPath]");
- }
- }
- public function display($tempPath) {
- if($this->oSmarty->template_exists($tempPath)){
- $this->oSmarty->display($tempPath);
- }else{
- require_once 'Zend/View/Exception.php';
- throw new Zend_View_Exception("For smarty, provided template is not exists [$tempPath]");
- }
- }
- protected function filterOutPut($outPut) {
- $aFilters = $this->getFilters();
- foreach($aFilters as $filterName){
- $oFilter = $this->getFilter($filterName);
- $outPut = call_user_func(array($oFilter, 'filter'), $outPut);
- }
- return $outPut ;
- }
- /**
- * Escapes a value for output in a view script.
- *
- * If escaping mechanism is one of htmlspecialchars or htmlentities, uses
- * {@link $_encoding} setting.
- *
- * @param mixed $var The output to escape.
- * @return mixed The escaped value.
- *
- */
- public function escape($var){
- if(is_string($var)){
- return parent::escape($var);
- }elseif(is_array($var)){
- return array_map(array(parent,"escape"), $var);
- }else{
- return $var ;
- }
- }
- /**
- * Set flag indicating if stream wrapper should be used if short_open_tag is off
- *
- * @param bool $flag
- * @return Zend_View
- */
- public function setUseStreamWrapper($flag)
- {
- $this->_useStreamWrapper = (bool) $flag;
- return $this;
- }
- /**
- * Should the stream wrapper be used if short_open_tag is off?
- *
- * @return bool
- */
- public function useStreamWrapper()
- {
- return $this->_useStreamWrapper;
- }
- /**
- * Includes the view script in a scope with only public $this variables.
- *
- * @param string The view script to execute.
- */
- protected function _run()
- {
- if ($this->_useViewStream && $this->useStreamWrapper()) {
- include 'zend.view://' . func_get_arg(0);
- } else {
- include func_get_arg(0);
- }
- }
- }
- Zendframework 1.6整合Smarty
- zendframework+smarty 配置好了
- 在zendframework中如果要采用smarty关闭view
- zendframework
- Zend Framework+Smarty 整合
- CodeIgniter整合smarty
- CI 里面整合Smarty
- zendframework2 与 smarty 整合
- yii整合smarty
- ci smarty 整合
- ci smarty 整合
- Codeigniter整合smarty
- Codeigniter轻松整合smarty
- YII2 整合smarty
- CI轻松整合smarty
- Yii1整合smarty
- yii2整合smarty
- Zend Framework整合smarty方法
- 在VC中调用 WebService (非托管-zhuan)
- LR中Windows默认计数器英文帮助
- 使用AJAX技术实现网页无闪自动局部刷新
- 简单的Ajax对象
- Association & Aggregation & Compositon 不同之处?
- Zendframework 1.6整合Smarty
- CVS安装与使用
- 服务器性能测试典型工具介绍
- SQL SERVER性能优化综述
- C#排序算法——基类的实现
- 世界500强企业员工的15种美德 之四:感恩
- Sql Excel Access 转换
- ARM 指令集摘要
- 修正linux下ls输出的目录颜色,让putty用起来更舒服