码迷,mamicode.com
首页 > 其他好文 > 详细

yaf twig配置

时间:2017-04-03 23:03:09      阅读:866      评论:0      收藏:0      [点我收藏+]

标签:ram   get   env   escape   ssi   $path   false   rip   调用   

1.安装 TWIG
composer require twig/twig
2.COMPOSER自动加载的引用
修改 BOOTSTRAP.PHP 增加
public function _initAutoload() {
require __DIR__ . "/../vendor/autoload.php";

}
3.新建ADAPTER
任意命名 需要实现Yaf_View_Interface接口

class Template_Adapter implements Yaf_View_Interface
{


private $vars;
private $path_view;

private $twig;
private $loader;
private $extraParams;

public function __construct($tmplPath = null, $extraParams = array())
{
if ($tmplPath) {
$this->path_view = $tmplPath;
}
$this->extraParams=$extraParams;

}

private function f()
{
if (!$this->loader) {
$this->loader = new Twig_Loader_Filesystem($this->path_view);
$this->twig = new Twig_Environment( $this->loader , $this->extraParams);
}



}

public function assign($spec, $value = null)
{

if (is_array($spec)) {
$this->vars = $spec;
return;
}

$this->vars[$spec] = $value;

}

public function display($tpl, $tpl_vars = null)
{
if (!is_null($tpl_vars) && is_array($tpl_vars)) {
$this->vars = $tpl_vars;
}
$this->f();
echo $this->twig->render($tpl,$this->vars);


}


public function render($tpl, $tpl_vars = null)
{
$this->display($tpl, $tpl_vars);
}


public function setScriptPath($template_dir)
{
if ($template_dir) {
$this->path_view = $template_dir;
}

}

public function getScriptPath()
{
return $this->path_view;
}


}


4.BOOTSTARP中增加TWIG引用



public function _initView(Yaf_Dispatcher $dispatcher){

$dispatcher->autoRender(false);


$ve= new Template_Adapter(APPLICATION_PATH.‘/view/‘,
[
‘path_cache‘ => APPLICATION_PATH.‘/viewcache/‘
]);*/


require ‘Template_Adapter.php‘;
$ve= new Template_Adapter(APPLICATION_PATH.‘/view/‘,[
‘debug‘ => false,
‘charset‘ => ‘UTF-8‘,
‘base_template_class‘ => ‘Twig_Template‘,
‘strict_variables‘ => false,
‘autoescape‘ => ‘html‘,
‘cache‘ => APPLICATION_PATH.‘/viewcache/‘,
‘auto_reload‘ => null,
‘optimizations‘ => -1]);
Yaf_Dispatcher::getInstance()->setView($ve);
}

5.控制器中调用
$this->getView()->display(‘a.html‘);

yaf twig配置

标签:ram   get   env   escape   ssi   $path   false   rip   调用   

原文地址:http://www.cnblogs.com/tttlan/p/6663761.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!