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

推荐订阅源

V
Visual Studio Blog
U
Unit 42
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
The GitHub Blog
The GitHub Blog
Microsoft Azure Blog
Microsoft Azure Blog
有赞技术团队
有赞技术团队
Stack Overflow Blog
Stack Overflow Blog
爱范儿
爱范儿
博客园 - 司徒正美
Vercel News
Vercel News
I
InfoQ
GbyAI
GbyAI
C
Check Point Blog
B
Blog RSS Feed
Martin Fowler
Martin Fowler
B
Blog
MyScale Blog
MyScale Blog
腾讯CDC
博客园 - Franky
Blog — PlanetScale
Blog — PlanetScale
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Hugging Face - Blog
Hugging Face - Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 三生石上(FineUI控件)

博客园 - 天天天蓝

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');