view_renderers.md 1.56 KB
Newer Older
1 2 3 4
Yii2 view renderers
===================

By default Yii uses PHP as template language but you can change it in your application.
5 6
The component responsible for rendering a view is called `view`. You can configure
a custom template engine as follows:
7 8 9 10

```php
array(
	'components' => array(
11 12 13 14 15 16
		'view' => array(
			'class' => 'yii\base\View',
			'renderer' => array(
				'class' => 'yii\renderers\TwigViewRenderer',
				// or 'class' => 'yii\renderers\SmartyViewRenderer',
			),
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
		),
	),
)
```

Twig
----

In order to use Twig you need to put you templates in files with extension `.twig`.
Also you need to specify this extension explicitly when calling `$this->render()`
or `$this->renderPartial()` from your controller.

Smarty
------

In order to use Smarty you need to put you templates in files with extension `.tpl`.
Also you need to specify this extension explicitly when calling `$this->render()`
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
or `$this->renderPartial()` from your controller.

Using multiple view renderers in a single application
-----------------------------------------------------

If you need multiple view renderers at the same time in a single application you
can use `CompositeViewRenderer` as follows:

```php
'components' => array(
	'view' => array(
		'class' => 'yii\base\View',
		'renderer' => array(
			'class' => 'yii\renderers\CompositeViewRenderer',
			'renderers' => array(
				'tpl' => array(
					'class' => 'yii\renderers\SmartyViewRenderer',
				),
				'twig' => array(
					'class' => 'yii\renderers\TwigViewRenderer',
				),
			),
			//'class' => 'yii\renderers\TwigViewRenderer',
		),
	),
),
```