title 3-5.twig模板引擎
之前我们用composer加载了一个数据库,现在我们用composer来加载一个php不叫牛逼的模板引擎 -- twig
1. 在composer.json里加入
*同样给代表对版本没有限制
2. 在基类里把模板引擎替换掉
更改display方法
public function display($file){
$file = APP.'/view/'.$file;
if(is_file($file)){
\Twig_Autoloader::register();
$loader = new \Twig_Loader_Filesystem(APP.'/view');
$twig = new \Twig_Environment($loader, array(
'cache' => LTY.'log/twig', //缓存
'debug' => DEBUG
));
$template = $twig->loadTemplate('index.html');
$template->display($this->assign?$this->assign:'');
}
}
然后去控制器调用
$data = 'hello word';
$this->assign('data',$data);
$this->display('index.html');
注意在html页面接值的时候用{{}}来接,而且不急$
twig还有一个比较牛逼的功能就是继承模板
{% extends "aa.html"%}
{% block content %}
{{data}}
{% endblock %}
在新建一个aa.html 文件
<html>
<body>
<header>header</header>
<content>
{% block content %}
{% endblock%}
</content>
<footer>footer</footer>
</body>
</html>
这样我们访问的时候就是