Browse Source

Polished up the basic discussion of template alternatives

tags/2.0.0-beta
Larry Ullman 11 years ago
parent
commit
c05477b141
  1. 36
      docs/guide/template.md

36
docs/guide/template.md

@ -3,8 +3,8 @@ Using template engines
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/). 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/).
The component responsible for rendering a view is called `view`. You can add The `view` component is responsible for rendering views. You can add
a custom template engines as follows: a custom template engines by reconfiguring this component's behavior:
```php ```php
array( array(
@ -26,15 +26,13 @@ array(
) )
``` ```
Note that Smarty and Twig are not bundled with Yii and you have to download and 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).
unpack these yourself and then specify `twigPath` and `smartyPath` respectively.
Twig Twig
---- ----
In order to use Twig you need to put you templates in files with extension `.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).
(or another one if configured differently). Unlike standard view files, when using Twig, you must include the extension when calling `$this->render()`
Also you need to specify this extension explicitly when calling `$this->render()`
or `$this->renderPartial()` from your controller: or `$this->renderPartial()` from your controller:
```php ```php
@ -43,25 +41,25 @@ echo $this->render('renderer.twig', array('username' => 'Alex'));
### Additional functions ### Additional functions
Additionally to regular Twig syntax the following is available in Yii: Yii adds the following construct to the standard Twig syntax:
```php ```php
<a href="{{ path('blog/view', {'alias' : post.alias}) }}">{{ post.title }}</a> <a href="{{ path('blog/view', {'alias' : post.alias}) }}">{{ post.title }}</a>
``` ```
path function calls `Html::url()` internally. Internally, the `path()` function calls Yii's `Html::url()` method.
### Additional variables ### Additional variables
- `app` = `\Yii::$app` Within Twig templates, you can also make use of these variables:
- `this` = current `View` object
- `app`, which equates to `\Yii::$app`
- `this`, which equates to the current `View` object
Smarty Smarty
------ ------
In order to use Smarty you need to put you templates in files with extension `.tpl` 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()`
(or another one if configured differently).
Also you need to specify this extension explicitly when calling `$this->render()`
or `$this->renderPartial()` from your controller: or `$this->renderPartial()` from your controller:
```php ```php
@ -70,16 +68,18 @@ echo $this->render('renderer.tpl', array('username' => 'Alex'));
### Additional functions ### Additional functions
Additionally to regular Smarty syntax the following is available in Yii: Yii adds the following construct to the standard Smarty syntax:
```php ```php
<a href="{path route='blog/view' alias=$post.alias}">{$post.title}</a> <a href="{path route='blog/view' alias=$post.alias}">{$post.title}</a>
``` ```
path function calls `Html::url()` internally. Internally, the `path()` function calls Yii's `Html::url()` method.
### Additional variables ### Additional variables
- `$app` = `\Yii::$app` Within Smarty templates, you can also make use of these variables:
- `$this` = current `View` object
- `$app`, which equates to `\Yii::$app`
- `$this`, which equates to the current `View` object

Loading…
Cancel
Save