Zend Framework中使用smarty

由于zend framework本身没有模板类的提供,而且采用php和html混在一起的,它的一些页面助手类,我感觉不是很方便,zend的工程师们估计也在加紧开发,没有模板类,导致我们在开发中非常不方便,毕竟现在很多系统开发中强调程序和模板分离,在模板中不要夹杂PHP代码,下面是我在网上找一个zend frameword和smarty结合的例子

我的Smarty.class.php是放在当前目录的libs目录下,可能你要按照你的实际情况来

<?php
require_once 'Zend/View/Interface.php';
require_once 'libs/Smarty.class.php';

class Zend_View_Smarty implements Zend_View_Interface
{

    protected $_smarty;


    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;
        }
    }

    public function getScriptPaths(){}
    public function setBasePath($path, $classPrefix = 'Zend_View'){}
    public function addBasePath($path, $classPrefix = 'Zend_View'){}


    public function getEngine()
    {
        return $this->_smarty;
    }


    public function setScriptPath($path)
    {
        if (is_readable($path)) {
            $this->_smarty->template_dir = $path;
            return;
        }

        throw new Exception('Invalid path provided');
    }


    public function __set($key, $val)
    {
        $this->_smarty->assign($key, $val);
    }


    public function __get($key)
    {
        return $this->_smarty->get_template_vars($key);
    }

    
    public function __isset($key)
    {
         return (null !== $this->_smarty->get_template_vars($key));
    }


    public function __unset($key)
    {
        $this->_smarty->clear_assign($key);
    }

    public function assign($spec, $value = null)
    {
        if (is_array($spec)) {
            $this->_smarty->assign($spec);
            return;
        }

        $this->_smarty->assign($spec, $value);
    }

    public function clearVars()
    {
        $this->_smarty->clear_all_assign();
    }

   
    public function render($name)
    {
        return $this->_smarty->fetch($name);
    }
}

?>

 

Zend Framework中使用smarty 有 0 个留言

我要说话

* * (不要加http://)