By default Yii uses PHP as template language, but you can configure it to support other rendering engines, such as Twig or Smarty.
The view
component is responsible for rendering views. You can add
a custom template engines by reconfiguring this component's behavior:
array(
'components' => array(
'view' => array(
'class' => 'yii\base\View',
'renderers' => array(
'tpl' => array(
'class' => 'yii\renderers\SmartyViewRenderer',
),
'twig' => array(
'class' => 'yii\renderers\TwigViewRenderer',
'twigPath' => '@app/vendors/Twig',
),
// ...
),
),
),
)
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).
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()
or $this->renderPartial()
from your controller:
echo $this->render('renderer.twig', array('username' => 'Alex'));
Yii adds the following construct to the standard Twig syntax:
<a href="{{ path('blog/view', {'alias' : post.alias}) }}">{{ post.title }}</a>
Internally, the path()
function calls Yii's Html::url()
method.
Within Twig templates, you can also make use of these variables:
app
, which equates to\Yii::$app
this
, which equates to the currentView
object
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 $this->renderPartial()
from your controller:
echo $this->render('renderer.tpl', array('username' => 'Alex'));
Yii adds the following construct to the standard Smarty syntax:
<a href="{path route='blog/view' alias=$post.alias}">{$post.title}</a>
Internally, the path()
function calls Yii's Html::url()
method.
Within Smarty templates, you can also make use of these variables:
$app
, which equates to\Yii::$app
$this
, which equates to the currentView
object