Installation consists of two parts: getting composer package and configuring an application.
The preferred way to install this extension is through composer.
php composer.phar require --prefer-dist yiisoft/yii2-twig
to the require section of your composer.json.
In order to start using Twig you need to configure
view component like the following:
[ 'components' => [ 'view' => [ 'class' => 'yii\web\View', 'renderers' => [ 'twig' => [ 'class' => 'yii\twig\ViewRenderer', 'cachePath' => '@runtime/Twig/cache', // Array of twig options: 'options' => [ 'auto_reload' => true, ], 'globals' => [ 'html' => ['class' => '\yii\helpers\Html'], ], 'uses' => ['yii\bootstrap'], ], // ... ], ], ], ]
After it's done you can create templates in files that have the
.twig extension (or use another file extension but
configure the component accordingly). Unlike standard view files, when using Twig you must include the extension
$this->render() controller call:
return $this->render('renderer.twig', ['username' => 'Alex']);