Commit b2a2853e by Larry Ullman

Very minor edits

parent e10939a4
Using template engines Using template engines
====================== ======================
By default Yii uses PHP as template language, but you can configure it to support other rendering engines, such as By default, Yii uses PHP as its template language, but you can configure Yii to support other rendering engines, such as
[Twig](http://twig.sensiolabs.org/) or [Smarty](http://www.smarty.net/). [Twig](http://twig.sensiolabs.org/) or [Smarty](http://www.smarty.net/).
The `view` component is responsible for rendering views. You can add a custom template engines by reconfiguring this The `view` component is responsible for rendering views. You can add a custom template engine by reconfiguring this
component's behavior: component's behavior:
```php ```php
...@@ -30,22 +30,21 @@ component's behavior: ...@@ -30,22 +30,21 @@ component's behavior:
] ]
``` ```
In the config above we're using Smarty and Twig. In order to get these extensions in your project you need to modify 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` to include your `composer.json` file to include them, too:
``` ```
"yiisoft/yii2-smarty": "*", "yiisoft/yii2-smarty": "*",
"yiisoft/yii2-twig": "*", "yiisoft/yii2-twig": "*",
``` ```
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 --preder-dist` in the command-line.
in `require` section and then run `composer update --preder-dist`.
Twig Twig
---- ----
To use Twig, you need to create templates in files with the `.twig` extension (or use another file extension but configure the component accordingly). 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 when calling `$this->render()` Unlike standard view files, when using Twig you must include the extension in your `$this->render()`
or `$this->renderPartial()` from your controller: or `$this->renderPartial()` controller calls:
```php ```php
echo $this->render('renderer.twig', ['username' => 'Alex']); echo $this->render('renderer.twig', ['username' => 'Alex']);
...@@ -70,8 +69,8 @@ Within Twig templates, you can also make use of these variables: ...@@ -70,8 +69,8 @@ Within Twig templates, you can also make use of these variables:
### Globals ### Globals
You can add global helpers or values via config's `globals`. It allows both using Yii helpers and setting your own You can add global helpers or values via the application configuration's `globals` variable. You can define both Yii helpers and your own
values: variables there:
```php ```php
'globals' => [ 'globals' => [
...@@ -80,7 +79,7 @@ values: ...@@ -80,7 +79,7 @@ values:
], ],
``` ```
Then in your template you can use it the following way: Once configured, in your template you can use the globals in the following way:
``` ```
Hello, {{name}}! {{ html.a('Please login', 'site/login') | raw }}. Hello, {{name}}! {{ html.a('Please login', 'site/login') | raw }}.
...@@ -88,7 +87,7 @@ Hello, {{name}}! {{ html.a('Please login', 'site/login') | raw }}. ...@@ -88,7 +87,7 @@ Hello, {{name}}! {{ html.a('Please login', 'site/login') | raw }}.
### Additional filters ### Additional filters
Additional filters may be added via config's `filters` option: Additional filters may be added via the application configuration's `filters` option:
```php ```php
'filters' => [ 'filters' => [
...@@ -96,7 +95,7 @@ Additional filters may be added via config's `filters` option: ...@@ -96,7 +95,7 @@ Additional filters may be added via config's `filters` option:
], ],
``` ```
Then in the template you can use Then in the template you can use:
``` ```
{{ model|jsonEncode }} {{ model|jsonEncode }}
...@@ -106,8 +105,8 @@ Then in the template you can use ...@@ -106,8 +105,8 @@ Then in the template you can use
Smarty Smarty
------ ------
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()` 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()` from your controller: or `$this->renderPartial()` controller calls:
```php ```php
echo $this->render('renderer.tpl', ['username' => 'Alex']); echo $this->render('renderer.tpl', ['username' => 'Alex']);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment