首页 > 其他 > 详细

yii2 使用twig 模板引擎

时间:2016-06-27 21:35:12      阅读:226      评论:0      收藏:0      [点我收藏+]

yii2 默认使用PHP 和html 混合的方式来写视图层,但我个人还是喜欢纯模板语言的方式。而且已经非常习惯使用twig的语法,最近想使用yii2进行开发,所以还是选择使用twig视图引擎。

    github 已经有人提供了这样的vendor ,可以直接comoser 配置一下进行使用。

composer.json 文件 require 添加 "yiisoft/yii2-twig": "*" 然后 composer update

   前往common/config 下 main.php 加上配置

[    ‘components‘ =>
      [        ‘view‘ => [           
                       ‘class‘ => ‘yii\web\View‘,           
                          ‘renderers‘ => [               
                           ‘tpl‘ => [                   
                            ‘class‘ => ‘yii\smarty\ViewRenderer‘,                   
                             //‘cachePath‘ => ‘@runtime/Smarty/cache‘,              
                               ],               
                            ‘twig‘ => [                   
                             ‘class‘ => ‘yii\twig\ViewRenderer‘,                
                             ‘cachePath‘ => ‘@runtime/Twig/cache‘,                 
                               // Array of twig options:                   
                                ‘options‘ => [                        
                                    ‘auto_reload‘ => true,                   
                                     ],                   
                             ‘globals‘ => [‘html‘ => ‘\yii\helpers\Html‘],          
                              ‘uses‘ => [‘yii\bootstrap‘],               
                        ],               
                                // ...         
                 ],      
              ],   
       ],]

    其中tpl 的配置是smarty 引擎。如果不使用smarty 可以不配置。接下来就可以去控制器技术分享下使用了。

return $this->render(‘test.twig‘, [‘test‘ => ‘hello,yii‘]);

   github地址:https://github.com/yiisoft/yii2/blob/master/docs/guide/tutorial-template-engines.md

yii2 使用twig 模板引擎

原文:http://www.cnblogs.com/hyh123/p/5621286.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!