template.md 2.62 KB
Newer Older
1 2 3
Using template engines
======================

Larry Ullman committed
4
By default Yii uses PHP as template language, but you can configure it to support other rendering engines, such as [Twig](http://twig.sensiolabs.org/) or [Smarty](http://www.smarty.net/).
5

6 7
The `view` component is responsible for rendering views. You can add
a custom template engines by reconfiguring this component's behavior:
8 9

```php
Alexander Makarov committed
10 11 12
[
	'components' => [
		'view' => [
Alexander Makarov committed
13
			'class' => 'yii\web\View',
Alexander Makarov committed
14 15
			'renderers' => [
				'tpl' => [
16
					'class' => 'yii\renderers\SmartyViewRenderer',
Alexander Makarov committed
17 18
				],
				'twig' => [
19 20
					'class' => 'yii\renderers\TwigViewRenderer',
					'twigPath' => '@app/vendors/Twig',
Alexander Makarov committed
21
				],
22
				// ...
Alexander Makarov committed
23 24 25 26
			],
		],
	],
]
27 28
```

29
Note that the Smarty and Twig packages themselves are not bundled with Yii. You must download them yourself. Then unpack the packages and place the resulting files in a logical location, such as the application's `protected/vendor` folder. Finally, specify the correct `smartyPath` or `twigPath`, as in the code above (for Twig).
30 31 32 33

Twig
----

34 35
To use Twig, you need to create templates in files with 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  when calling `$this->render()`
36 37 38
or `$this->renderPartial()` from your controller:

```php
Alexander Makarov committed
39
echo $this->render('renderer.twig', ['username' => 'Alex']);
40 41 42 43
```

### Additional functions

44
Yii adds the following construct to the standard Twig syntax:
45 46 47 48 49

```php
<a href="{{ path('blog/view', {'alias' : post.alias}) }}">{{ post.title }}</a>
```

50
Internally, the `path()` function calls Yii's `Html::url()` method.
51 52 53

### Additional variables

54 55 56 57
Within Twig templates, you can also make use of these variables:

- `app`, which equates to `\Yii::$app`
- `this`, which equates to the current `View` object
58 59 60 61

Smarty
------

62
To use Smarty, you need to create templates in files with the `.tpl` extension (or use another file extension but configure the component accordingly). Unlike standard view files, when using Smarty, you must include the extension  when calling `$this->render()`
63 64 65
or `$this->renderPartial()` from your controller:

```php
Alexander Makarov committed
66
echo $this->render('renderer.tpl', ['username' => 'Alex']);
67 68 69 70
```

### Additional functions

71
Yii adds the following construct to the standard Smarty syntax:
72 73 74 75 76

```php
<a href="{path route='blog/view' alias=$post.alias}">{$post.title}</a>
```

77
Internally, the `path()` function calls Yii's `Html::url()` method.
78 79 80

### Additional variables

81 82 83 84
Within Smarty templates, you can also make use of these variables:

- `$app`, which equates to `\Yii::$app`
- `$this`, which equates to the current `View` object
85