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模板引擎的详细内容。