使用zend framework架構搭建網站(三) –smarty擴充

來源:互聯網
上載者:User
使用zend framework架構搭建網站(三) --smarty擴充2008/07/24 11:02 P.M.
     今天總結一下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/compile

dir.cache        = ../app/cache

三,四兩步可以參見hi.baidu.com/billdkj/blog/item/bec0b4fc8db6c0fbfd037f34.html《使用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,
後台不用變了,只需要將視圖檔案改變就行了

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.