yaf 中使用smarty

composer中引入

{
    "require": {
        "smarty/smarty": "~3.1"
    },
    "repositories": {
        "packagist": {
            "type": "composer",
            "url": "https://mirrors.aliyun.com/composer/"
        }
    }
}

入口文件引入vendor自动加载

define('APP_PATH', dirname(dirname(__FILE__))); // 获取根目录
require(APP_PATH . '/vendor/autoload.php'); // 引入自动加载

Bootstrap中引入smarty

/**
 * 将 Yaf 框架默认模板引擎替换为 Smarty。
 *
 * @param Yaf_Dispatcher $dispatcher
 * @return void
 */
public function _initSmarty(Yaf_Dispatcher $dispatcher)
{
    //$smarty = new CommonSmartyAdapter(null , Yaf_Application::app()->getConfig()->smarty);
    // smarty运用
    $smarty = new CommonSmartyAdapter(null , App::getConfig('smarty'));
    Yaf_Dispatcher::getInstance()->setView($smarty);
}

配置

; Smarty 模板引擎配置。
smarty.left_delimiter   = "{{"
smarty.right_delimiter  = "}}"
smarty.template_dir     = APP_PATH "/app/admin/views/"
smarty.compile_dir      = APP_PATH "/logs/cache/compile"
smarty.cache_dir        = APP_PATH "/logs/cache/"
smarty.caching          = 0
smarty.escape_html      = true

入口类中获取smarty对象

$this->_view       = $this->getView();

封装assign方法

/**
 * 模板传值(this->_view->assign())。
 *
 * -- 该方法是封装了 Yaf_View 提供的 assign() 方法。
 * 
 * @param  mixed  $name  字符串或者关联数组, 如果为字符串, 则$value不能为空, 此字符串代表要分配的变量名. 如果为数组, 则$value须为空, 此参数为变量名和值的关联数组.
 * @param  mixed  $value 分配的模板变量值
 * @return bool
 */
protected function assign($name, $value = null)
{
    return $this->_view->assign($name, $value);
}

具体使用

$this->assign('admin_name','猛虎');
<div class="message"> {{$admin_name}} 管理系统</div>
原文地址:https://www.cnblogs.com/jiqing9006/p/12103729.html