首先我们也是要先下载twig模板引擎,现在先在composer.json里面写一行代码
然后在cmd 里面跟前两篇一样再下载一下composer
twig模板引擎和smtary差不多,我们需要去修改一下display,就像下面的一样
public function dispaly($file)
{
$file = APP. '\app\views\\'.$file;
//$file = './app/views/'.$file;
//p($file);exit();
if(is_file($file))
{
extract($this->assign);
\Twig_Autoloader::register();
$loader = new \Twig_Loader_Filesystem(APP. '\app\views\\');
//dump($loader);exit();
$twig = new \Twig_Environment($loader, array(
'cache' => './log/twig',
'debug' => DEBUG
));
$template = $twig ->loadTemplate('index.html');
$template ->display($this->assign?$this->assign:'');
}
}
这个时候可能有人就想问了一个问题就是下图一样
这个就是我们twig的缓存路径
然后我们需要去控制器里面来加载我们的v层的index.html页面
<?php
namespace app\controller;
use core\lib\model;
class indexController extends \core\imooc
{
public function index()
{
$data = 'hello world';
$this ->assign('data',$data);
$this ->dispaly('index.html');
这个时候我们也要根据twig模板的格式来写我们的v层的东西
{% extends "layout.html" %}
{% block content %}
{{data}}
{% endblock %}
这样我们的twig模板就好了。