MVC简单搭建之twig模板引擎

来源:互联网 发布:linux批量安装 编辑:程序博客网 时间:2024/06/04 18:47

  


关于twig呢,我是通过composer来安装的

http://twig.sensiolabs.org/    这个网址是有关于twig的


首先呢是要在 composer.json 里加入   

   "twig/twig":"*"

然后在终端里面进行一些操作

       进入到根目录之后

       composer update

然后在imooc.php里

    public function display($file)    {        $file_one=$file;//        echo $file;die;        $file = APP.'/views/'.$file;        if(is_file($file))        {            //extract($this->assign);            //include $file;            //替换twig模板引擎            \Twig_Autoloader::register();            $loader = new \Twig_Loader_Filesystem(APP.'/views');            $twig = new \Twig_Environment($loader, array(                'cache' => IMOOC.'/log/twig',                'debug' => DEBUG            ));            $template = $twig->loadTemplate($file_one);            $template->display($this->assign?$this->assign:array());        }    }

这里的部分代码就是在之前的网址里

     


做到这里就可以使用了,


我在视图层建了一个文件  layout.php

<html><body><header>header</header><content>    {% block content %}    {% endblock %}</content><footer>footer</footer></body></html>
这算是一个公共模板,在这里定义样式等,

引入的时候

   {% extends "layout.html" %}

  {% block content %}    {% endblock %}

就可以啦

0 0