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!