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.

 

 

  1. <?php
  2. /**
  3.  *  Application base on Zend framework
  4.  *
  5.  *  Note:
  6.  *      - The default template postfix is 'html'.
  7.  *      - The default view controller root directory is [MAIN_DIR].'/zendView/views'
  8.  *      - The default zend Framework package was install in the 'PHP/PEAR/zendFramework'
  9.  *      - Zend framework default does not checking POST,GET,COOKIE variable, So we should do it here.
  10.  ***/
  11. /*
  12.     * Global function define
  13.         - [对象调试输出] print_obj(object);
  14. */
  15. function print_obj($o){
  16.     echo "<p><pre>";
  17.     var_dump($o);
  18.     echo "</pre></p>";
  19. }
  20. /*
  21.     * Basically define.
  22. */
  23. set_include_path(ini_get("include_path").";c:/webServer/php/PEAR/ZendFramework");
  24. define("MAIN_DIR",dirname(__FILE__));
  25. /*
  26.     * include file
  27.         - [前台控制器] Zend/Controller/Front.php
  28.         - [基于Smarty的扩展类,与Zend view整合] Zend/Smarty/ZendSmarty.php
  29.         - [助手经济人] Zend/Controller/Action/HelperBroker.php
  30.         - [视图控制器] Zend/Controller/Action/Helpoer/ViewRenderer.php
  31.         - [配置文件管理器] Zend/Config/Ini.php
  32.         - [模块配置文件] [MAIN_DIR]/zendConfig/module.config.php
  33.         - [Smarty模板配置参数] [MAIN_DIR]/zendConfig/base.config.php
  34. */
  35. require_once 'Zend/Controller/Front.php';
  36. require_once("Zend/Smarty/ZendSmarty.php");
  37. require_once("Zend/Controller/Action/HelperBroker.php");
  38. require_once("Zend/Controller/Action/Helper/ViewRenderer.php");
  39. require_once("Zend/Config/Ini.php");
  40. require_once("Totuba/PageStructureController.php");
  41. $confModule = include_once(MAIN_DIR."/zendConfig/module.config.php");
  42. $confGlobal = include_once(MAIN_DIR."/zendConfig/base.config.php");
  43. $confContentRegion = include_once(MAIN_DIR."/zendConfig/content_region.config.php");
  44. $confPageStructureMode = include_once(MAIN_DIR."/zendConfig/page_structure_mode.config.php");
  45. /*
  46.     * Program section
  47. */
  48. $oFront = Zend_Controller_Front::getInstance();
  49. //---  Set module path configration  ---
  50. $oFront->setControllerDirectory($confModule);
  51. //---  Zend framework view floor defination  ---
  52. $aViewRendererOptions = array(
  53.     "viewBasePathSpec"=>MAIN_DIR."/zendView/views/:module",
  54.     "viewSuffix"=>"html"
  55.     //"viewScriptPathSpec"=>":moduleDir/views/scripts/:controller/:action.:suffix",
  56.     //"viewScriptPathNoControllerSpec"=>"default/views/scripts/:controller/:action.:suffix",
  57.     //"viewSuffix"=>"phtml"
  58. );
  59. $aPageStructureControllerOptions = array(
  60.     "contentRegionConfigration"=>$confContentRegion,
  61.     "pageStructureModeConfigration"=>$confPageStructureMode,
  62.     "applicationDirectory"=>MAIN_DIR."/application"
  63. );
  64. $oViewRender = new Zend_Controller_Action_Helper_ViewRenderer(new Zend_Smarty_ZendSmarty(array(),$confGlobal['smarty']),$aViewRendererOptions);
  65. $oPageStructureController = new Totuba_PageStructureController($oViewRender,$aPageStructureControllerOptions);
  66. Zend_Controller_Action_HelperBroker::addHelper($oViewRender);
  67. Zend_Controller_Action_HelperBroker::addHelper($oPageStructureController);
  68. //Totuba_PageStructureController::setOptions($aPageStructureControllerOptions);
  69. //Totuba_PageStructureController::testOptions();
  70. //---  Zend framework view floor defination [end]  ---  */
  71. $oRequest = $oFront->getRequest();
  72. //---  Before despatch, deleting unused global variables  ---
  73. unset($confModule,$confGlobal);
  74. $oFront->dispatch();

 

用smarty 扩展的 Zend_View 原程序如下:

 

 

  1. <?php
  2. /*
  3.     smarty implement Zend_View_Interface
  4.     
  5.     File name: ZendSmarty.php
  6.     
  7.     Date:2008-9-3
  8.     Author: Jason Guan
  9.     
  10.     Email: Guan.xingmin@hotmail.com
  11.     这个类继承自Zend_View_Abstract 类,父类设计是按照Zend_View要求来设计的,这个不符合Smarty的运行环境和要求,
  12.     因此,在该类中,要重写部分父类的方法,支持Smarty,并且扩展该类,使其可以灵活支持模板管理,和路径准确定位。
  13.     对该类的调用是通过Zend_Controller_Action_Helper_ViewRenderer类来实现的,系统真正对模板或视图层的操作是通过
  14.     这个类来实现的,因此,对Zend_Controller_Action_Helper_ViewRenderer类也有一定扩展功能的必要使其可以完全支持
  15.     Smarty和内部系统对视图层操作的要求。
  16.     对视图层的操作默认情况是 module/controller/view/scripts/controller/action.suffix,
  17.     但是应该可以通过指定模块控制器和行为,自由调用任何一个模块的视图。同时在调用任何视图的同时应该可以执行相应的
  18.     方法,也就是模块方法,为视图模板提供数据,这个也是系统内部模块自由调用的需求。
  19. */
  20. include_once("zend/Smarty/Smarty.class.php");
  21. include_once("zend/View/Abstract.php");
  22. include_once("Zend/Config/Ini.php");
  23. class Zend_Smarty_ZendSmarty extends Zend_View_Abstract{
  24.     public $oSmarty = null ;
  25.     //---  view base folder  ---
  26.     /*
  27.         If the application view structure is base on module, the conf_baseDirectory is module/views
  28.         Otherwise this conf_baseDirectory is "folder to application view"/
  29.     */
  30.     public $conf_baseDirectory = "";
  31.     /**
  32.      * Whether or not to use streams to mimic short tags
  33.      * @var bool
  34.      */
  35.     private $_useViewStream = false;
  36.     /**
  37.      * Whether or not to use stream wrapper if short_open_tag is false
  38.      * @var bool
  39.      */
  40.     private $_useStreamWrapper = false;
  41.     /**
  42.      * Constructor
  43.      *
  44.      * Register Zend_View_Stream stream wrapper if short tags are disabled.
  45.      * 
  46.      * @param  array $config 
  47.      * @param  $smartyConf [Zend_Config_Ini:object]: the smarty configration data
  48.      * @return void 
  49.      * 
  50.      */
  51.     public function __construct($config = array(),$aSmartyConf)
  52.     {
  53.         $this->_useViewStream = (bool) ini_get('short_open_tag') ? false : true;
  54.         if ($this->_useViewStream) {
  55.             if (!in_array('zend.view', stream_get_wrappers())) {
  56.                 require_once 'Zend/View/Stream.php';
  57.                 stream_wrapper_register('zend.view''Zend_View_Stream');
  58.             }
  59.         }
  60.         if (array_key_exists('useStreamWrapper'$config)) {
  61.             $this->setUseStreamWrapper($config['useStreamWrapper']);
  62.         }
  63.         parent::__construct($config);
  64.         
  65.         $this->oSmarty = new Smarty();
  66.         
  67.         $this->setSmartyConfigration($aSmartyConf);
  68.     }
  69.     public function init(){
  70.         
  71.     }
  72.     
  73.     public function &getSmarty(){
  74.         return $this->oSmarty ;
  75.     }
  76.     public function setSmartyConfigration($aConf){
  77.         
  78.         if(!is_array($aConf)){
  79.             require_once 'Zend/View/Exception.php';
  80.             throw new Zend_View_Exception('Setting smarty configration error,the parameter is not a valid Array!'$this);
  81.         }
  82.         foreach($aConf as $k=>$v){
  83.             
  84.             if(isset($this->oSmarty->$k) && ( "_" != substr($k,0,1))){
  85.                 $this->oSmarty->$k = $v ;
  86.             }
  87.         }
  88.         return $this ;
  89.     }
  90.     /**
  91.      * Directly assigns a variable to the view script.
  92.      *
  93.      * Checks first to ensure that the caller is not attempting to set a
  94.      * protected or private member (by checking for a prefixed underscore); if
  95.      * not, the public member is set; otherwise, an exception is raised.
  96.      *
  97.      * @param string $key The variable name.
  98.      * @param mixed $val The variable value.
  99.      * @return void
  100.      * @throws Zend_View_Exception if an attempt to set a private or protected
  101.      * member is detected
  102.      *
  103.      *  This is a copy from Zend/View/Abstract.php 
  104.      */
  105.     public function __set($key$val)
  106.     {
  107.         if ('_' != substr($key, 0, 1)) {
  108.             $this->$key = $val;
  109.             return;
  110.         }
  111.         //---  This is the modibied rows  ---
  112.         $this->oSmarty->assign($key,$val);
  113.         require_once 'Zend/View/Exception.php';
  114.         throw new Zend_View_Exception('Setting private or protected class members is not allowed'$this);
  115.     }
  116.     /**
  117.      * Allows unset() on object properties to work
  118.      *
  119.      * @param string $key
  120.      * @return void
  121.      */
  122.     public function __unset($key)
  123.     {
  124.         if ('_' != substr($key, 0, 1) && isset($this->$key)) {
  125.             unset($this->$key);
  126.         }
  127.         $this->oSmarty->clear_assign($key);
  128.     }
  129.     public function _setEscape($spec)
  130.     {
  131.         if(is_string($spec))
  132.             //return parent::$this->_escape = $spec;
  133.         return $this;
  134.     }
  135.     public function assign($spec$value = null){
  136.         
  137.         if(is_string($spec)){
  138.             $this->oSmarty->assign($spec,$value);
  139.         }
  140.         elseif(is_array($spec)){
  141.             $this->oSmarty->assign($spec);
  142.         }
  143.         else{
  144.             require_once 'Zend/View/Exception.php';
  145.             throw new Zend_View_Exception('assign() expects a string or array, got '.gettype($var));
  146.         }
  147.             
  148.     }
  149.     public function getVars(){
  150.         return $this->oSmarty->get_template_vars();
  151.     }
  152.     public function getVar($assignedVariableName){
  153.         return $this->oSmarty->get_template_vars($assignedVariableName);
  154.     }
  155.     public function clearVars(){
  156.         $this->oSmarty->clear_all_assign();
  157.     }
  158.     public function clearVar($assignedVariableName){
  159.         $this->oSmarty->clear_assign($assignedVariableName);    
  160.     }
  161.     
  162.     /*
  163.         This method is only used for Zend Framework internal view needing
  164.     */
  165.     public function render($name$options=NULL){
  166.         $outPutFile = $this->_script($name);
  167.         
  168.         $a = $this->oSmarty->fetch($outPutFile);
  169.         
  170.          return $this->filterOutPut( $a ); // filter output
  171.     }
  172.     
  173.     /*
  174.         Generally the tempPath is module/controller/action.html 
  175.         But it is allows to set other path
  176.     */
  177.     public function fetch($tempPath) {
  178.         if($this->oSmarty->template_exists($tempPath)){
  179.             return $this->oSmarty->fetch($tempPath);
  180.         }else{
  181.             require_once 'Zend/View/Exception.php';
  182.             throw new Zend_View_Exception("For smarty, provided template is not exists [$tempPath]");
  183.         }
  184.     }
  185.     
  186.     public function display($tempPath) {
  187.         if($this->oSmarty->template_exists($tempPath)){
  188.             $this->oSmarty->display($tempPath);
  189.         }else{
  190.             require_once 'Zend/View/Exception.php';
  191.             throw new Zend_View_Exception("For smarty, provided template is not exists [$tempPath]");
  192.         }
  193.     }
  194.     protected function filterOutPut($outPut) {
  195.         $aFilters = $this->getFilters();
  196.         foreach($aFilters as $filterName){
  197.             $oFilter = $this->getFilter($filterName);
  198.             $outPut = call_user_func(array($oFilter'filter'), $outPut);
  199.         }
  200.         return $outPut ;
  201.     }
  202.     
  203.      /**
  204.      * Escapes a value for output in a view script.
  205.      *
  206.      * If escaping mechanism is one of htmlspecialchars or htmlentities, uses
  207.      * {@link $_encoding} setting.
  208.      *
  209.      * @param mixed $var The output to escape.
  210.      * @return mixed The escaped value.
  211.      *
  212.      */
  213.     public function escape($var){
  214.         if(is_string($var)){
  215.             return parent::escape($var);
  216.         }elseif(is_array($var)){
  217.             return array_map(array(parent,"escape"), $var);
  218.         }else{
  219.             return $var ;
  220.         }
  221.     }
  222.     /**
  223.      * Set flag indicating if stream wrapper should be used if short_open_tag is off
  224.      * 
  225.      * @param  bool $flag 
  226.      * @return Zend_View
  227.      */
  228.     public function setUseStreamWrapper($flag)
  229.     {
  230.         $this->_useStreamWrapper = (bool) $flag;
  231.         return $this;
  232.     }
  233.     /**
  234.      * Should the stream wrapper be used if short_open_tag is off?
  235.      * 
  236.      * @return bool
  237.      */
  238.     public function useStreamWrapper()
  239.     {
  240.         return $this->_useStreamWrapper;
  241.     }
  242.     /**
  243.      * Includes the view script in a scope with only public $this variables.
  244.      *
  245.      * @param string The view script to execute.
  246.      */
  247.     protected function _run()
  248.     {
  249.         if ($this->_useViewStream && $this->useStreamWrapper()) {
  250.             include 'zend.view://' . func_get_arg(0);
  251.         } else {
  252.             include func_get_arg(0);
  253.         }
  254.     }
  255.     
  256. }
原创粉丝点击