-
Notifications
You must be signed in to change notification settings - Fork 5
/
ViewRenderer.php
102 lines (90 loc) · 2.78 KB
/
ViewRenderer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
/**
* View.php
* @author Revin Roman
* @link https://rmrevin.com
*/
namespace rmrevin\yii\pug;
use Pug\Pug;
use Yii;
use yii\base\View;
use yii\helpers\FileHelper;
/**
* Class ViewRenderer
* @package rmrevin\yii\pug
*/
class ViewRenderer extends \yii\base\ViewRenderer
{
/**
* @var string the directory or path alias pointing to where Pug cache will be stored. Set to false to disable
* templates cache.
*/
public $cachePath = '@runtime/pug/cache';
/**
* @var array Pug options.
* @see https://github.com/pug-php/pug
*/
public $options = [
'prettyprint' => false,
'extension' => '.pug',
'upToDateCheck' => true,
];
/**
* @var array Custom filters.
* Keys of the array are names to call in template, values are names of functions or static methods of some class.
* Example: `['rot13' => 'str_rot13', 'jsonEncode' => '\yii\helpers\Json::encode']`.
* In the template you can use it like this: `{{ 'test'|rot13 }}` or `{{ model|jsonEncode }}`.
*/
public $filters = [];
/**
* @var Pug pug environment object that renders pug templates
*/
public $pug;
public function init()
{
$cachePath = empty($this->cachePath) ? false : Yii::getAlias($this->cachePath);
if (!empty($cachePath) && !file_exists($cachePath)) {
FileHelper::createDirectory($cachePath);
}
if (!empty($cachePath) && !is_readable($cachePath)) {
throw new Exception(\Yii::t('app', 'Pug cache path is not readable.'));
}
if (!empty($cachePath) && !is_writeable($cachePath)) {
throw new Exception(\Yii::t('app', 'Pug cache path is not writable.'));
}
$this->pug = new Pug(array_merge([
'cache' => $cachePath,
], $this->options));
// Adding custom filters
if (!empty($this->filters)) {
foreach ($this->filters as $name => $handler) {
$this->addFilter($name, $handler);
}
}
}
/**
* Renders a view file.
*
* This method is invoked by [[View]] whenever it tries to render a view.
* Child classes must implement this method to render the given view file.
*
* @param View $view the view object used for rendering the file.
* @param string $file the view file.
* @param array $params the parameters to be passed to the view file.
*
* @return string the rendering result
*/
public function render($view, $file, $params)
{
return $this->pug->render($file, $params);
}
/**
* Adds custom filter
* @param string $name
* @param callable $handler
*/
public function addFilter($name, $handler)
{
$this->pug->filter($name, $handler);
}
}