Yaf整合Smarty模板引擎

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

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

<?php

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


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值