1. 云栖社区>
  2. PHP教程>
  3. 正文

Yaf整合Smarty模板引擎

作者:用户 来源:互联网 时间:2017-12-01 11:42:20

模板引擎smarty整合yaf

Yaf整合Smarty模板引擎 - 摘要: 本文讲的是Yaf整合Smarty模板引擎, 将Smarty文件夹放在library目录下面 在Smarty目录下面添加Adapter.php,代码如下: <?phprequire "Smarty.class.php";class Smarty_Adapter impleme

将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/"


以上是云栖社区小编为您精心准备的的内容,在云栖社区的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索模板 , 引擎 , smarty , 整合 yaf ,以便于您获取更多的相关知识。