惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Vercel News
Vercel News
F
Fortinet All Blogs
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
GbyAI
GbyAI
MongoDB | Blog
MongoDB | Blog
Jina AI
Jina AI
aimingoo的专栏
aimingoo的专栏
I
InfoQ
IT之家
IT之家
罗磊的独立博客
Blog — PlanetScale
Blog — PlanetScale
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
The Cloudflare Blog
爱范儿
爱范儿
Microsoft Azure Blog
Microsoft Azure Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
美团技术团队
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
B
Blog RSS Feed
G
Google Developers Blog
大猫的无限游戏
大猫的无限游戏
博客园_首页
Engineering at Meta
Engineering at Meta
Martin Fowler
Martin Fowler

博客园 - 天天天蓝

JDK 国内源 jenkins spring cloud springboot spring 文章索引 java文章索引 小程序 AWAIT ASYNC ES6转ES5解决 centos vsftpd centos nginx linux 修改配色 mac 常用技巧 ryzen nvidia hackintosh mysql count 主键之坑 git命令 MYSQL 注释 1.YAF 的安装 yaf nginx 设置 ubuntu 16 阿里云 vsftpd win10下 homestead 安装
yaf twig配置
天天天蓝 · 2017-04-03 · via 博客园 - 天天天蓝
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');