Yaf整合Smarty模板引擎

来源:互联网 发布:用编程代码说我爱你 编辑:程序博客网 时间:2024/06/05 03:57

将Smarty文件夹放在library目录下面

在Smarty目录下面添加Adapter.php,代码如下:

<?phprequire "Smarty.class.php";class Smarty_Adapter implements Yaf_View_Interface{    /**     * Smarty object     * @var Smarty     */    public $_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');    }     /**     * Retrieve the current template directory     *     * @return string     */    public function getScriptPath()    {        return $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);    }     /**     * 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));    }        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, $value = NULL) {        return $this->_smarty->fetch($name);    }    public function display($name, $value = NULL) {        echo $this->_smarty->fetch($name);    }}/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */?>

在application.ini中添加配置。如下:

[php] view plain copy
 print?
  1. smarty.left_delimiter   = "<{"    
  2. smarty.right_delimiter  = "}>"    
  3. smarty.template_dir     = APP_PATH "/application/views/"  
  4. smarty.compile_dir      = APP_PATH "/application/cache/compile"  
  5. smarty.cache_dir        = APP_PATH "/application/cache/"  

使用Bootstrap.php文件

 在入口文件中:

[php] view plain copy
 print?
  1. $app->bootstrap()->run();  

然后编辑Bootstrap.php文件。如下:

public function _initSmarty(Yaf_Dispatcher $dispatcher){    Yaf_Loader::import("Smarty/Adapter.php");    $smarty = new Smarty_Adapter(null, Yaf_Application::app()->getConfig()->smarty);    // 设置模板语言包函数    $smarty->getEngine()->register_function("getlang","Lang::smartygetlang");    $dispatcher->setView($smarty);}


设置标签

[smarty : yaf]smarty.left_delimiter   = "{"smarty.right_delimiter  = "}"application.view.ext="html"smarty.template_dir     = APP_PATH "/application/views/"smarty.compile_dir      = APP_PATH "/application/cache/compile"smarty.cache_dir        = APP_PATH "/application/cache/"