Zend Framework架構Smarty擴充實現方法,zendsmarty_PHP教程

來源:互聯網
上載者:User

Zend Framework架構Smarty擴充實現方法,zendsmarty


本文執行個體講述了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 ,檔案的內容如下:

<?php/*** Zend_View_Interface*/require_once 'Zend/View/Interface.php';/*** Smarty */require_once("smarty/Smarty.class.php");/*** 建立Smarty視圖*/class Zend_View_Smarty implements Zend_View_Interface{  /**   * Smarty object   * @var Smarty   */  protected $_smarty;  /**   * Constructor   *   * @param string $tmplPath   * @param array $extraParams   * @return void   */  public function __construct($tmplPath = null, $extraParams = array())  {    $this->_smarty = new Smarty;    if (null !== $tmplPath) {      $this->setScriptPath($tmplPath);    }    foreach ($extraParams as $key => $value) {      $this->_smarty->$key = $value;    }  }  /**   * Return the template engine object     *   * @return Smarty   */  public function getEngine()  {    return $this->_smarty;  }  /**   * Set the path to the templates   *   * @param string $path The directory to set as the path.   * @return void   */  public function setScriptPath($path)  {    if (is_readable($path)) {      $this->_smarty->template_dir = $path;      return;    }    throw new Exception('Invalid path provided');  }  /**  * set smarty緩衝  * @author lengfeng  */  public function setCompilePath($path){    if (is_readable($path)) {      $this->_smarty->compile_dir = $path;      return;    }    throw new Exception('Invalid path provided');      }  /**  * set smarty 編譯後文檔  * @author lengfeng  */  public function setCachePath($path){    if (is_readable($path)) {      $this->_smarty->cache_dir = $path;      return;    }    throw new Exception('Invalid path provided');      }  /**   * Retrieve the current template directory   *   * @return string   */  public function getScriptPaths()  {    return array($this->_smarty->template_dir);  }  /**   * Alias for setScriptPath   *   * @param string $path   * @param string $prefix Unused   * @return void   */  public function setBasePath($path, $prefix = 'Zend_View')  {    return $this->setScriptPath($path);  }  /**   * Alias for setScriptPath   *   * @param string $path   * @param string $prefix Unused   * @return void   */  public function addBasePath($path, $prefix = 'Zend_View')  {    return $this->setScriptPath($path);  }  /**   * Assign a variable to the template   *   * @param string $key The variable name.   * @param mixed $val The variable value.   * @return void   */  public function __set($key, $val)  {    $this->_smarty->assign($key, $val);  }  /**   * Retrieve an assigned variable   *   * @param string $key The variable name.   * @return mixed The variable value.   */  public function __get($key)  {    return $this->_smarty->get_template_vars($key);  }  /**   * Allows testing with empty() and isset() to work   *   * @param string $key   * @return boolean   */  public function __isset($key)  {     return (null !== $this->_smarty->get_template_vars($key));  }  /**   * Allows unset() on object properties to work   *   * @param string $key   * @return void   */  public function __unset($key)  {    $this->_smarty->clear_assign($key);  }  /**   * Assign variables to the template   *   * Allows setting a specific key to the specified value, OR passing an array   * of key => value pairs to set en masse.   *   * @see __set()   * @param string|array $spec The assignment strategy to use (key or array of key   * => value pairs)   * @param mixed $value (Optional) If assigning a named variable, use this   * as the value.   * @return void   */  public function assign($spec, $value = null)  {    if (is_array($spec)) {      $this->_smarty->assign($spec);      return;    }    $this->_smarty->assign($spec, $value);  }  /**   * Clear all assigned variables   *   * Clears all variables assigned to Zend_View either via {@link assign()} or   * property overloading ({@link __get()}/{@link __set()}).   *   * @return void   */  public function clearVars()  {    $this->_smarty->clear_all_assign();  }  /**   * Processes a template and returns the output.   *   * @param string $name The template to process.   * @return string The output.   */  public function render($name)  {    return $this->_smarty->fetch($name);  }  /**   * 設定是否產生緩衝   * 如果沒有參數,預設為true   */  public function setCache($bool){     if (isset($bool)) {      $this->_smarty->caching = $bool;      return;    }  }}

三.在app檔案夾下建立cache ,compile 檔案夾

四.在config.ini 設定檔中加入

dir.compile    = ../app/compiledir.cache    = ../app/cache

三,四兩步可以參見前面關於zendfreamwork架構搭建網站相關教程

五.在application.php 檔案中添加

/*** 初始化smarty視圖**/private function _initSmartyView(){    $view = new Zend_View_Smarty();    $view->setBasePath($this->_pathConfig->dir->viewBase);    $view->setScriptPath($this->_pathConfig->dir->viewBase."/scripts");    $view->setCompilePath($this->_pathConfig->dir->compile);    $view->setCachePath($this->_pathConfig->dir->cache);    $smarty=$view->getEngine();    $smarty->caching=false;    $smarty->debugging = true;    $smarty->compile_check = true;        $smarty->left_delimiter = "<{"; //定義標示符    $smarty->right_delimiter = "}>";    $registry = Zend_Registry::getInstance();    $registry->set('smartyview',$smarty); //smarty對象    $registry->set('sview',$view);          }

並在 函數 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,後台不用變了,只需要將視圖檔案改變就行了

更多關於zend相關內容感興趣的讀者可查看本站專題:《Zend FrameWork架構入門教程》、《php優秀開發架構總結》、《Yii架構入門及常用技巧總結》、《ThinkPHP入門教程》、《php物件導向程式設計入門教程》、《php+mysql資料庫操作入門教程》及《php常見資料庫操作技巧匯總》

希望本文所述對大家基於Zend Framework架構的PHP程式設計有所協助。

您可能感興趣的文章:

  • Zend Framework架構教程之Zend_Db_Table_Rowset用法執行個體分析
  • Zend Framework教程之Zend_Db_Table_Row用法執行個體分析
  • Zend Framework教程之Zend_Db_Table用法詳解
  • Zend Framework教程之Zend_Form組件實現表單提交並顯示錯誤提示的方法
  • Zend Framework開發入門經典教程
  • Zend Framework架構路由機制程式碼分析
  • Zend Framework實現具有準系統的留言本(附demo源碼下載)
  • Zend Framework實現將session儲存在memcache中的方法
  • Zend Framework分頁類用法詳解
  • Zend Framework實現多檔案上傳功能執行個體
  • Zend Framework入門之環境配置及第一個Hello World樣本(附demo源碼下載)
  • Zend Framework教程之串連資料庫並執行增刪查的方法(附demo源碼下載)
  • Zend Framework教程之Zend_Db_Table表關聯執行個體詳解

http://www.bkjia.com/PHPjc/1113718.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1113718.htmlTechArticleZend Framework架構Smarty擴充實現方法,zendsmarty 本文執行個體講述了Zend Framework架構Smarty擴充實現方法。分享給大家供大家參考,具體如下: 今天總...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.