-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViewRenderer.php
130 lines (100 loc) · 3.27 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/**
* @link https://github.com/kfreiman/yii2-lightncandy
* @copyright Copyright (c) Kirill Freiman
* @license MIT <http://opensource.org/licenses/MIT>
*/
namespace kfreiman\lightncandy;
use Yii;
use yii\base\View;
use yii\base\ViewRenderer as BaseViewRenderer;
use LightnCandy;
class ViewRenderer extends BaseViewRenderer
{
public $options = [];
public $basedir; // only dirname($file) by default
public $cache_preffix = 'LightnCandy_';
public $extension = ['.handlebars', '.mustache'];
public $flags;
public function init()
{
if (YII_ENV_DEV) {
$this->flags = $this->flags |
LightnCandy::FLAG_ERROR_EXCEPTION
// | LightnCandy::FLAG_RENDER_DEBUG
;
}
$this->flags = $this->flags |
LightnCandy::FLAG_INSTANCE |
LightnCandy::FLAG_NOESCAPE |
LightnCandy::FLAG_SPVARS |
LightnCandy::FLAG_RUNTIMEPARTIAL |
LightnCandy::FLAG_HANDLEBARSJS |
LightnCandy::FLAG_BARE;
}
/**
* 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)
{
$this->basedir[] = dirname($file);
$renderer = $this->getTemplateRenderer($file);
return $renderer($params);
}
protected function getTemplateRenderer($file)
{
// include all {{> partials }}
// $content = $this->includePartial($file);
// var_dump($content);
// $hash = md5($content);
// $key = $this->cache_preffix.$hash;
// $phpStr = Yii::$app->cache->get($key);
// if ($phpStr === false) {
$phpStr = $this->getPhpStr($file);
// $phpStr = file_get_contents(dirname(__FILE__).'/test.php');
// echo $this->getPhpStr($file);
// exit;
// Yii::$app->cache->set($key, $phpStr);
// }
return eval($phpStr.';');
}
protected function getPhpStr($file)
{
return LightnCandy::compile(
'{{>'. pathinfo($file, PATHINFO_FILENAME) .'}}', // render as partial
[
'flags' => $this->flags,
'basedir' => $this->basedir,
'fileext' => $this->extension,
] + $this->options
);
}
protected function includePartial($file)
{
$content = file_get_contents($file);
return preg_replace_callback('/{{>(.{1,})}}/', [$this, "getPartial"], $content);
}
protected function getPartial($matchs)
{
var_dump('expression');
$partName = trim($matchs[1]);
foreach ($this->basedir as $dir) {
foreach ($this->extension as $ext) {
$fn = "$dir/$partName$ext";
var_dump($fn);
if (file_exists($fn)) {
return $this->includePartial($fn);
}
}
}
}
}