A-A+

Zend Framework框架Smarty扩展实现方法

2021年07月21日 我爱编程 暂无评论

这篇文章主要介绍了Zend Framework框架Smarty扩展实现方法,结合实例形式较为详细的分析了Zend Framework框架Smarty扩展的具体步骤与相关设置技巧,需要的朋友可以参考下。

本文实例讲述了Zend Framework框架Smarty扩展实现方法,分享给大家供大家参考,具体如下:

今天总结一下ZF框架中扩展Smarty模板的方法,在ZF帮助文档中已经有比较详细的介绍,在这我稍微多说一些。

一.将smarty的核心文件包放在lib文件夹下,文件包中要包括(internals/,plugins/,Config_File.class.php,Smarty.class.php,Smarty_Compiler.class.php,debug.tpl).

二.在Zend/View下添加文件:Smarty.php ,文件的内容如下:

  1. <?php
  2. /**
  3. * Zend_View_Interface
  4. */
  5. require_once 'Zend/View/Interface.php';
  6. /**
  7. * Smarty 
  8. */
  9. require_once("smarty/Smarty.class.php");
  10. /**
  11. * 创建Smarty视图
  12. */
  13. class Zend_View_Smarty implements Zend_View_Interface
  14. {
  15.   /**
  16.    * Smarty object
  17.    * @var Smarty
  18.    */
  19.   protected $_smarty;
  20.   /**
  21.    * Constructor
  22.    *
  23.    * @param string $tmplPath
  24.    * @param array $extraParams
  25.    * @return void
  26.    */
  27.   public function __construct($tmplPath = null, $extraParams = array())
  28.   {
  29.     $this->_smarty = new Smarty;
  30.     if (null !== $tmplPath) {
  31.       $this->setScriptPath($tmplPath);
  32.     }
  33.     foreach ($extraParams as $key => $value) {
  34.       $this->_smarty->$key = $value;
  35.     }
  36.   }
  37.   /**
  38.    * Return the template engine object  
  39.    *
  40.    * @return Smarty
  41.    */
  42.   public function getEngine()
  43.   {
  44.     return $this->_smarty;
  45.   }
  46.   /**
  47.    * Set the path to the templates
  48.    *
  49.    * @param string $path The directory to set as the path.
  50.    * @return void
  51.    */
  52.   public function setScriptPath($path)
  53.   {
  54.     if (is_readable($path)) {
  55.       $this->_smarty->template_dir = $path;
  56.       return;
  57.     }
  58.     throw new Exception('Invalid path provided');
  59.   }
  60.   /**
  61.   * set smarty缓存
  62.   * @author lengfeng
  63.   */
  64.   public function setCompilePath($path){
  65.     if (is_readable($path)) {
  66.       $this->_smarty->compile_dir = $path;
  67.       return;
  68.     }
  69.     throw new Exception('Invalid path provided');
  70.   }
  71.   /**
  72.   * set smarty 编译后文档
  73.   * @author lengfeng
  74.   */
  75.   public function setCachePath($path){
  76.     if (is_readable($path)) {
  77.       $this->_smarty->cache_dir = $path;
  78.       return;
  79.     }
  80.     throw new Exception('Invalid path provided');
  81.   }
  82.   /**
  83.    * Retrieve the current template directory
  84.    *
  85.    * @return string
  86.    */
  87.   public function getScriptPaths()
  88.   {
  89.     return array($this->_smarty->template_dir);
  90.   }
  91.   /**
  92.    * Alias for setScriptPath
  93.    *
  94.    * @param string $path
  95.    * @param string $prefix Unused
  96.    * @return void
  97.    */
  98.   public function setBasePath($path$prefix = 'Zend_View')
  99.   {
  100.     return $this->setScriptPath($path);
  101.   }
  102.   /**
  103.    * Alias for setScriptPath
  104.    *
  105.    * @param string $path
  106.    * @param string $prefix Unused
  107.    * @return void
  108.    */
  109.   public function addBasePath($path$prefix = 'Zend_View')
  110.   {
  111.     return $this->setScriptPath($path);
  112.   }
  113.   /**
  114.    * Assign a variable to the template
  115.    *
  116.    * @param string $key The variable name.
  117.    * @param mixed $val The variable value.
  118.    * @return void
  119.    */
  120.   public function __set($key$val)
  121.   {
  122.     $this->_smarty->assign($key$val);
  123.   }
  124.   /**
  125.    * Retrieve an assigned variable
  126.    *
  127.    * @param string $key The variable name.
  128.    * @return mixed The variable value.
  129.    */
  130.   public function __get($key)
  131.   {
  132.     return $this->_smarty->get_template_vars($key);
  133.   }
  134.   /**
  135.    * Allows testing with empty() and isset() to work
  136.    *
  137.    * @param string $key
  138.    * @return boolean
  139.    */
  140.   public function __isset($key)
  141.   {
  142.      return (null !== $this->_smarty->get_template_vars($key));
  143.   }
  144.   /**
  145.    * Allows unset() on object properties to work
  146.    *
  147.    * @param string $key
  148.    * @return void
  149.    */
  150.   public function __unset($key)
  151.   {
  152.     $this->_smarty->clear_assign($key);
  153.   }
  154.   /**
  155.    * Assign variables to the template
  156.    *
  157.    * Allows setting a specific key to the specified value, OR passing an array
  158.    * of key => value pairs to set en masse.
  159.    *
  160.    * @see __set()
  161.    * @param string|array $spec The assignment strategy to use (key or array of key
  162.    * => value pairs)
  163.    * @param mixed $value (Optional) If assigning a named variable, use this
  164.    * as the value.
  165.    * @return void
  166.    */
  167.   public function assign($spec$value = null)
  168.   {
  169.     if (is_array($spec)) {
  170.       $this->_smarty->assign($spec);
  171.       return;
  172.     }
  173.     $this->_smarty->assign($spec$value);
  174.   }
  175.   /**
  176.    * Clear all assigned variables
  177.    *
  178.    * Clears all variables assigned to Zend_View either via {@link assign()} or
  179.    * property overloading ({@link __get()}/{@link __set()}).
  180.    *
  181.    * @return void
  182.    */
  183.   public function clearVars()
  184.   {
  185.     $this->_smarty->clear_all_assign();
  186.   }
  187.   /**
  188.    * Processes a template and returns the output.
  189.    *
  190.    * @param string $name The template to process.
  191.    * @return string The output.
  192.    */
  193.   public function render($name)
  194.   {
  195.     return $this->_smarty->fetch($name);
  196.   }
  197.   /**
  198.    * 设置是否生成缓存
  199.    * 如果没有参数,默认为true
  200.    */
  201.   public function setCache($bool){
  202.      if (isset($bool)) {
  203.       $this->_smarty->caching = $bool;
  204.       return;
  205.     }
  206.   }
  207. }

三.在app文件夹下创建cache ,compile 文件夹

四.在config.ini 配置文件中加入

dir.compile    = ../app/compile

dir.cache    = ../app/cache

三,四两步可以参见前面关于zendfreamwork框架搭建网站相关教程

五.在application.php 文件中添加

  1. /**
  2. * 初始化smarty视图
  3. *
  4. */
  5. private function _initSmartyView()
  6. {
  7.     $view = new Zend_View_Smarty();
  8.     $view->setBasePath($this->_pathConfig->dir->viewBase);
  9.     $view->setScriptPath($this->_pathConfig->dir->viewBase."/scripts");
  10.     $view->setCompilePath($this->_pathConfig->dir->compile);
  11.     $view->setCachePath($this->_pathConfig->dir->cache);
  12.     $smarty=$view->getEngine();
  13.     $smarty->caching=false;
  14.     $smarty->debugging = true;
  15.     $smarty->compile_check = true;
  16.     $smarty->left_delimiter = "<{"//定义标示符
  17.     $smarty->right_delimiter = "}>";
  18.     $registry = Zend_Registry::getInstance();
  19.     $registry->set('smartyview',$smarty); //smarty对象
  20.     $registry->set('sview',$view);
  21. }

并在 函数 init()中加入

$this->_initSmartyView();

六.在Controller中调用

因为已经将对象注册,所以可以如下调用:

$view = Zend_Registry::getInstance()->get("smartyview");

注意这是smarty对象,使用smarty的那些语法,比如 $view->assign("user","root");

$view = Zend_Registry::getInstance()->get("sview");

这是zf的view对象,按zf中的那些方法用,不用改变。

按这样,你如果要将以前写的代码改为用smaty,后台不用变了,只需要将视图文件改变就行了

给我留言

Copyright © 四季博客 保留所有权利.   Theme  Ality

用户登录