tutorial-template-engines.md 4.17 KB
Newer Older
1 2 3
Using template engines
======================

Qiang Xue committed
4 5
> Note: This chapter is under development.

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

Larry Ullman committed
9
The `view` component is responsible for rendering views. You can add a custom template engine by reconfiguring this
10
component's behavior:
11 12

```php
Alexander Makarov committed
13
[
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
    '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',
                    //'options' => [], /*  Array of twig options */
                    'globals' => ['html' => '\yii\helpers\Html'],
                ],
                // ...
            ],
        ],
    ],
Alexander Makarov committed
32
]
33 34
```

Larry Ullman committed
35 36
In the code above, both Smarty and Twig are configured to be useable by the view files. But in order to get these extensions into your project, you need to also modify
your `composer.json` file to include them, too:
37 38 39 40 41

```
"yiisoft/yii2-smarty": "*",
"yiisoft/yii2-twig": "*",
```
42
That code would be added to the `require` section of `composer.json`. After making that change and saving the file, you can install the extensions by running `composer update --prefer-dist` in the command-line.
43 44 45 46

Twig
----

Larry Ullman committed
47 48 49
To use Twig, you need to 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 in your `$this->render()`
or `$this->renderPartial()` controller calls:
50 51

```php
Alexander Makarov committed
52
echo $this->render('renderer.twig', ['username' => 'Alex']);
53 54
```

Alexander Makarov committed
55
### Additional syntax
56

Alexander Makarov committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
Yii adds some extra syntax constructs additionally to standard Twig ones.


###

{{registerAssetBundle('AppAsset')}} - Registers asset bundle of a given name


### Forms

```
{% set form = form_begin({ ... }) %}
{{ form.field(...) }}
{% form.end() %}
```


#### Getting URL for a route

There are two functions you can use for URLs:
77 78 79

```php
<a href="{{ path('blog/view', {'alias' : post.alias}) }}">{{ post.title }}</a>
Alexander Makarov committed
80
<a href="{{ url('blog/view', {'alias' : post.alias}) }}">{{ post.title }}</a>
81 82
```

Alexander Makarov committed
83
`path` generates relative URL while `url` generates absolute one. Internally both are using [[\yii\helpers\Url]].
84 85 86

### Additional variables

87 88 89 90
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
91

92 93
### Globals

Larry Ullman committed
94 95
You can add global helpers or values via the application configuration's `globals` variable. You can define both Yii helpers and your own
variables there:
96 97 98

```php
'globals' => [
99 100
    'html' => '\yii\helpers\Html',
    'name' => 'Carsten',
101 102 103
],
```

Larry Ullman committed
104
Once configured, in your template you can use the globals in the following way:
105 106

```
yupe committed
107
Hello, {{name}}! {{ html.a('Please login', 'site/login') | raw }}.
108 109 110 111
```

### Additional filters

Larry Ullman committed
112
Additional filters may be added via the application configuration's `filters` option:
113 114 115

```php
'filters' => [
116
    'jsonEncode' => '\yii\helpers\Json::encode',
117 118 119
],
```

Larry Ullman committed
120
Then in the template you can use:
121 122 123 124 125 126

```
{{ model|jsonEncode }}
```


127 128 129
Smarty
------

Larry Ullman committed
130 131
To use Smarty, you need to create templates in files that have 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 in your `$this->render()`
or `$this->renderPartial()` controller calls:
132 133

```php
Alexander Makarov committed
134
echo $this->render('renderer.tpl', ['username' => 'Alex']);
135 136 137 138
```

### Additional functions

139
Yii adds the following construct to the standard Smarty syntax:
140 141 142 143 144

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

145
Internally, the `path()` function calls Yii's `Url::to()` method.
146 147 148

### Additional variables

149 150 151 152
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
153