1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of slick/template package |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Slick\Template\Engine; |
11
|
|
|
|
12
|
|
|
use Slick\Template\Exception\ParserException; |
13
|
|
|
use Slick\Template\TemplateEngineInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Twig |
17
|
|
|
* |
18
|
|
|
* @package Slick\Template\Engine |
19
|
|
|
* @author Filipe Silva <[email protected]> |
20
|
|
|
* |
21
|
|
|
* @property array $locations A list of paths for template files. |
22
|
|
|
* @property string $cache Absolute path for compiled templates. |
23
|
|
|
* @property bool $debug Set debug mode and display the generated nodes. |
24
|
|
|
* @property string $charset The charset used by the templates. |
25
|
|
|
* @property string $baseTemplateClass |
26
|
|
|
* The base template class to use for generated templates. |
27
|
|
|
* @property bool $autoReload |
28
|
|
|
* Recompile the template whenever the source code changes. |
29
|
|
|
* @property bool $strictVariables |
30
|
|
|
* When false it silently ignore invalid variables (variables and or |
31
|
|
|
* attributes/methods that do not exist) and replace them with a null |
32
|
|
|
* value. |
33
|
|
|
* @property bool $autoEscape HTML auto-escaping |
34
|
|
|
* @property int $optimizations |
35
|
|
|
* A flag that indicates which optimizations to apply (default |
36
|
|
|
* to -1 -- all optimizations are enabled; set it to 0 to disable). |
37
|
|
|
* |
38
|
|
|
* @property \Twig_Loader_Filesystem $loader |
39
|
|
|
* @property \Twig_Environment $twigEnvironment |
40
|
|
|
* @property-write \Twig_Template $template |
41
|
|
|
* |
42
|
|
|
*/ |
43
|
|
|
class Twig extends AbstractEngine |
44
|
|
|
{ |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @readwrite |
48
|
|
|
* @var \Twig_Loader_Filesystem |
49
|
|
|
*/ |
50
|
|
|
protected $loader; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var array |
54
|
|
|
*/ |
55
|
|
|
private $optionsMap = [ |
56
|
|
|
'debug' => 'debug', |
57
|
|
|
'autoEscape' => 'autoescape', |
58
|
|
|
'strictVariables' => 'strict_variables', |
59
|
|
|
'autoReload' => 'auto_reload', |
60
|
|
|
'cache' => 'cache', |
61
|
|
|
'baseTemplateClass' => 'base_template_class', |
62
|
|
|
'charset' => 'charset', |
63
|
|
|
'optimizations' => 'optimizations' |
64
|
|
|
]; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @readwrite |
68
|
|
|
* @var \Twig_Environment |
69
|
|
|
*/ |
70
|
|
|
protected $twigEnvironment; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @write |
74
|
|
|
* @var \Twig_Template |
75
|
|
|
*/ |
76
|
|
|
protected $template; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @readwrite |
80
|
|
|
* @var string|false |
81
|
|
|
*/ |
82
|
|
|
protected $cache = false; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @readwrite |
86
|
|
|
* @var string |
87
|
|
|
*/ |
88
|
|
|
protected $charset = 'utf8'; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @readwrite |
92
|
|
|
* @var string |
93
|
|
|
*/ |
94
|
|
|
protected $baseTemplateClass = 'Twig_Template'; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @readwrite |
98
|
|
|
* @var bool |
99
|
|
|
*/ |
100
|
|
|
protected $autoReload = false; |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @readwrite |
104
|
|
|
* @var bool |
105
|
|
|
*/ |
106
|
|
|
protected $strictVariables = false; |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @readwrite |
110
|
|
|
* @var bool|string |
111
|
|
|
*/ |
112
|
|
|
protected $autoEscape = true; |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @readwrite |
116
|
|
|
* @var int |
117
|
|
|
*/ |
118
|
|
|
protected $optimizations = -1; |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @readwrite |
122
|
|
|
* @var bool |
123
|
|
|
*/ |
124
|
|
|
protected $debug = false; |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @readwrite |
128
|
|
|
* @var array |
129
|
|
|
*/ |
130
|
|
|
protected $locations = []; |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Parses the source template code. |
134
|
|
|
* |
135
|
|
|
* @param string $source The template to parse |
136
|
|
|
* |
137
|
|
|
* @return TemplateEngineInterface|self|$this |
138
|
|
|
* |
139
|
|
|
* @throws ParserException If any error occurs parsing the template |
140
|
|
|
*/ |
141
|
4 |
|
public function parse($source) |
142
|
|
|
{ |
143
|
|
|
try { |
144
|
4 |
|
$this->template = $this->getTwigEnvironment() |
145
|
4 |
|
->loadTemplate($source); |
146
|
4 |
|
} catch (\Exception $caught) { |
147
|
2 |
|
throw new ParserException( |
148
|
2 |
|
"Template parse error: ".$caught->getMessage(), |
149
|
2 |
|
0, |
150
|
|
|
$caught |
151
|
2 |
|
); |
152
|
|
|
} |
153
|
2 |
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Processes the template with data to produce the final output. |
158
|
|
|
* |
159
|
|
|
* @param mixed $data The data that will be used to process the view. |
160
|
|
|
* |
161
|
|
|
* @return string Returns processed output string. |
162
|
|
|
*/ |
163
|
4 |
|
public function process($data = array()) |
164
|
|
|
{ |
165
|
|
|
try { |
166
|
4 |
|
return $this->template->render($data); |
167
|
2 |
|
} catch (\Exception $caught) { |
168
|
2 |
|
throw new ParserException( |
169
|
2 |
|
"Template process error: ".$caught->getMessage(), |
170
|
2 |
|
0, |
171
|
|
|
$caught |
172
|
2 |
|
); |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Sets the list of available locations for template files. |
178
|
|
|
* |
179
|
|
|
* @param array $locations |
180
|
|
|
* |
181
|
|
|
* @return TemplateEngineInterface|self|$this |
182
|
|
|
*/ |
183
|
14 |
|
public function setLocations(array $locations) |
184
|
|
|
{ |
185
|
14 |
|
$this->locations = $locations; |
186
|
14 |
|
return $this; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Returns the source template engine |
192
|
|
|
* |
193
|
|
|
* @return \Twig_Environment |
194
|
|
|
*/ |
195
|
2 |
|
public function getSourceEngine() |
196
|
|
|
{ |
197
|
2 |
|
return $this->getTwigEnvironment(); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* Gets the twig environment object |
202
|
|
|
* |
203
|
|
|
* @return \Twig_Environment |
204
|
|
|
*/ |
205
|
6 |
|
protected function getTwigEnvironment() |
206
|
|
|
{ |
207
|
6 |
|
if (null == $this->twigEnvironment) { |
208
|
2 |
|
$this->twigEnvironment = new \Twig_Environment( |
209
|
2 |
|
$this->getLoader(), |
210
|
2 |
|
$this->getOptions() |
211
|
2 |
|
); |
212
|
2 |
|
} |
213
|
6 |
|
return $this->twigEnvironment; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Creates a file system loader |
218
|
|
|
* |
219
|
|
|
* @return \Twig_Loader_Filesystem |
220
|
|
|
*/ |
221
|
4 |
|
protected function getLoader() |
222
|
|
|
{ |
223
|
4 |
|
if (null == $this->loader) { |
224
|
2 |
|
$this->loader = new \Twig_Loader_Filesystem( |
225
|
2 |
|
$this->locations |
226
|
2 |
|
); |
227
|
2 |
|
} |
228
|
4 |
|
return $this->loader; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Returns current configured options |
233
|
|
|
* |
234
|
|
|
* @return array |
235
|
|
|
*/ |
236
|
2 |
|
protected function getOptions() |
237
|
|
|
{ |
238
|
2 |
|
$options = []; |
239
|
2 |
|
foreach($this->optionsMap as $property => $name) { |
240
|
2 |
|
$options[$name] = $this->$property; |
241
|
2 |
|
} |
242
|
2 |
|
return $options; |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|