在框架中view层多用模板显示,之前用原生的不太简洁,效率有点低。
本篇介绍twig模板的应用。
模板安装
composer
我所用的就是这种
1
| composer require twig/twig:~1.0
|
git安装
1 2 3
| git clone git://github.com/twigphp/Twig.git cd Twig composer install
|
应用
template
在相应位置下简历templates文件夹
1
| mkdir templates && cd templates && touch TwigTemplate.php
|
在TwigTemplate.php新建类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| class TwigTemplate { public $view;
public $data;
public $twig;
public $path = APPLICATION_PATH . '/modules/www/views/';
/** * Twig constructor. * @param $view * @param $data */ public function __construct($view, $data) { if($GLOBALS['basePathName']){ $this->path=$GLOBALS['basePathName']; } $loader = new Twig_Loader_Filesystem($this->path); $this->twig = new Twig_Environment($loader, array( 'cache' => APPLICATION_PATH . '/../cache/views/', 'debug' => true ));
$this->view = $view; $this->data = $data;
}
/** * @param $view * @param array $data * @return Twig */ public static function render($view, $data = array()) {
return new TwigTemplate($view, $data);
}
public function __destruct() { $this->twig->display($this->view, $this->data); } }
|
实现
Controller层
1 2 3 4
| $data=array( 'test' => 'hello!', ); return TwigTemplate::render('index.html',$data);
|
View层
参考链接
- https://www.kancloud.cn/yunye/twig-cn/159457
- https://www.cnblogs.com/evai/p/6244043.html