Home  >  Article  >  PHP Framework  >  How to use twig template engine in yii framework

How to use twig template engine in yii framework

王林
王林Original
2020-03-11 13:42:342434browse

How to use twig template engine in yii framework

yii2 By default, a mixture of PHP and HTML is used to write the view layer. If you are very accustomed to using twig syntax, you can choose to use the twig view engine.

Github has already provided such a vendor, which can be directly configured with composer for use.

composer.json file require add "yiisoft/yii2-twig": "*" and then composer update

(recommended tutorial: yii framework)

Go to main.php under common/config and add the configuration

[    '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'],              
                        ],              
                                // ...        
                 ],     
              ],  
       ],]

The configuration of tpl is the smarty engine. If you don't use smarty, you don't need to configure it. Then you can use it under the controller.

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

The above is the detailed content of How to use twig template engine in yii framework. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn