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

How to use twig template engine in yii framework

王林
Release: 2020-03-11 13:42:34
Original
2437 people have browsed it

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'],              
                        ],              
                                // ...        
                 ],     
              ],  
       ],]
Copy after login

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']);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template