Yii 2 Pug (ex Jade) extension
This extension provides a view renderer for Pug templates for Yii framework 2.0 applications.
Support
Installation
The preferred way to install this extension is through composer.
Either run
composer require pug/yii2
or add
"pug/yii2": "^1.0",
to the require
section of your composer.json
file.
Configure
return [
// ...
'components' => [
// ...
'view' => [
// ...
'renderers' => [
'pug' => 'Pug\\Yii\\ViewRenderer',
],
],
],
];
You can also use other pug renderer like phug or tale-pug
return [
// ...
'components' => [
// ...
'view' => [
// ...
'renderers' => [
'pug' => [
'class' => 'Pug\\Yii\\ViewRenderer',
'renderer' => 'Phug\\Renderer',
],
],
],
],
];
Phug and Pug-php (the default renderer) are automatically installed when you install the last version of pug/yii2
, for other pug renderer, replace the renderer class and include it.
For example, for Tale-pug, use composer require talesoft/tale-pug
then replace 'Tale\\Pug\\Renderer'
with 'Tale\\Pug\\Renderer'
in the config example above.
Cache path and View path
By default, this extension uses '@runtime/pug/cache'
to store compiled templates, and looks in '@app/views'
to find the templates to compile.
You can change this, by specifying cachePath
and/or viewPath
in the configuration, for example:
return [
// ...
'components' => [
// ...
'view' => [
// ...
'renderers' => [
'pug' => [
'class' => 'Pug\\Yii\\ViewRenderer',
'cachePath' => '@app/runtime/Pugcache',
'viewPath' => '@app/themes/dark/templates/views',
],
],
],
],
];
pug/yii
provide 2 global variables: app
(Yii::$app
) and view
(view path given to ->render()
in your controller). It imply that:
- If you set app or view shared variables, they will be erased by those system values.
- If you pass app or view locals to the
->render()
method they will have the precedence on system values.
To avoid such name conflict, you can pack into on object and store in a shared variable with a name you chose:
return [
// ...
'components' => [
// ...
'view' => [
// ...
'renderers' => [
'pug' => [
'class' => 'Pug\\Yii\\ViewRenderer',
// ...
'systemVariable' => '_yii',
],
],
],
],
];
With this config, app and view are no longer reserved names (can be used for your own locals or shared variables), only _yii is, and so you can get app and view like this:
h1=_yii.view.defaultExtension
p=_yii.app.version
Or if you set the expressionLanguage
option to "php"
:
h1=$_yii->view->defaultExtension
p=$_yii->app->getVersion()
Credits
This solution merge both project rmrevin/yii2-pug (original fork that support pug-php 2) and jacmoe/yii2-tale-pug (tale-pug and tale-jade Yii2 solution) and finally bring support for pug-php 3 and phug engines.