MVC简单搭建之twig模板引擎

  


关于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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值