yii框架怎么使用twig模板引擎

2020-11-02,

/2020/11/fbf7c0b2.jpg

yii2 默认使用PHP 和html 混合的方式来写视图层,如果您已经非常习惯使用twig的语法,可以选择使用twig视图引擎。

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

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

(推荐教程:yii框架)

前往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']);

以上就是yii框架怎么使用twig模板引擎的详细内容,更多请关注北冥有鱼其它相关文章!

本文转载自【PHP中文网】,希望能给您带来帮助,苟日新、日日新、又日新,生命不息,学习不止。

《yii框架怎么使用twig模板引擎.doc》

下载本文的Word格式文档,以方便收藏与打印。