|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace League\Plates\Template; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use League\Plates\Engine; |
|
7
|
|
|
use LogicException; |
|
8
|
|
|
use Throwable; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Container which holds template data and provides access to template functions. |
|
12
|
|
|
*/ |
|
13
|
|
|
class Template |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Instance of the template engine. |
|
17
|
|
|
* @var Engine |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $engine; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* The name of the template. |
|
23
|
|
|
* @var Name |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $name; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* The data assigned to the template. |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $data = array(); |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* An array of section content. |
|
35
|
|
|
* @var array |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $sections = array(); |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* The name of the template layout. |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $layoutName; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* The data assigned to the template layout. |
|
47
|
|
|
* @var array |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $layoutData; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Create new Template instance. |
|
53
|
|
|
* @param Engine $engine |
|
54
|
|
|
* @param string $name |
|
55
|
|
|
*/ |
|
56
|
116 |
|
public function __construct(Engine $engine, $name) |
|
57
|
|
|
{ |
|
58
|
116 |
|
$this->engine = $engine; |
|
59
|
116 |
|
$this->name = new Name($engine, $name); |
|
60
|
|
|
|
|
61
|
116 |
|
$this->data($this->engine->getData($name)); |
|
62
|
116 |
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Magic method used to call extension functions. |
|
66
|
|
|
* @param string $name |
|
67
|
|
|
* @param array $arguments |
|
68
|
|
|
* @return mixed |
|
69
|
|
|
*/ |
|
70
|
8 |
|
public function __call($name, $arguments) |
|
71
|
|
|
{ |
|
72
|
8 |
|
return $this->engine->getFunction($name)->call($this, $arguments); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Alias for render() method. |
|
77
|
|
|
* @throws \Throwable |
|
78
|
|
|
* @throws \Exception |
|
79
|
|
|
* @return string |
|
80
|
|
|
*/ |
|
81
|
4 |
|
public function __toString() |
|
82
|
|
|
{ |
|
83
|
4 |
|
return $this->render(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Assign or get template data. |
|
88
|
|
|
* @param array $data |
|
89
|
|
|
* @return mixed |
|
90
|
|
|
*/ |
|
91
|
116 |
|
public function data(array $data = null) |
|
92
|
|
|
{ |
|
93
|
116 |
|
if (is_null($data)) { |
|
94
|
4 |
|
return $this->data; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
116 |
|
$this->data = array_merge($this->data, $data); |
|
98
|
116 |
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Check if the template exists. |
|
102
|
|
|
* @return boolean |
|
103
|
|
|
*/ |
|
104
|
100 |
|
public function exists() |
|
105
|
|
|
{ |
|
106
|
100 |
|
return $this->name->doesPathExist(); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Get the template path. |
|
111
|
|
|
* @return string |
|
112
|
|
|
*/ |
|
113
|
96 |
|
public function path() |
|
114
|
|
|
{ |
|
115
|
96 |
|
return $this->name->getPath(); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Render the template and layout. |
|
120
|
|
|
* @param array $data |
|
121
|
|
|
* @throws \Throwable |
|
122
|
|
|
* @throws \Exception |
|
123
|
|
|
* @return string |
|
124
|
|
|
*/ |
|
125
|
92 |
|
public function render(array $data = array()) |
|
126
|
|
|
{ |
|
127
|
92 |
|
$this->data($data); |
|
128
|
92 |
|
unset($data); |
|
129
|
92 |
|
extract($this->data); |
|
130
|
|
|
|
|
131
|
92 |
|
if (!$this->exists()) { |
|
132
|
4 |
|
throw new LogicException( |
|
133
|
4 |
|
'The template "' . $this->name->getName() . '" could not be found at "' . $this->path() . '".' |
|
134
|
3 |
|
); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
try { |
|
138
|
88 |
|
$level = ob_get_level(); |
|
139
|
88 |
|
ob_start(); |
|
140
|
|
|
|
|
141
|
88 |
|
include $this->path(); |
|
142
|
|
|
|
|
143
|
68 |
|
$content = ob_get_clean(); |
|
144
|
|
|
|
|
145
|
68 |
|
if (isset($this->layoutName)) { |
|
146
|
16 |
|
$layout = $this->engine->make($this->layoutName); |
|
147
|
16 |
|
$layout->sections = array_merge($this->sections, array('content' => $content)); |
|
148
|
16 |
|
$content = $layout->render($this->layoutData); |
|
149
|
12 |
|
} |
|
150
|
|
|
|
|
151
|
68 |
|
return $content; |
|
152
|
20 |
|
} catch (Throwable $e) { |
|
|
|
|
|
|
153
|
5 |
|
while (ob_get_level() > $level) { |
|
154
|
5 |
|
ob_end_clean(); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
5 |
|
throw $e; |
|
158
|
15 |
|
} catch (Exception $e) { |
|
159
|
15 |
|
while (ob_get_level() > $level) { |
|
160
|
15 |
|
ob_end_clean(); |
|
161
|
15 |
|
} |
|
162
|
|
|
|
|
163
|
15 |
|
throw $e; |
|
164
|
|
|
} |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* Set the template's layout. |
|
169
|
|
|
* @param string $name |
|
170
|
|
|
* @param array $data |
|
171
|
|
|
* @return null |
|
172
|
|
|
*/ |
|
173
|
16 |
|
public function layout($name, array $data = array()) |
|
174
|
|
|
{ |
|
175
|
16 |
|
$this->layoutName = $name; |
|
176
|
16 |
|
$this->layoutData = $data; |
|
177
|
16 |
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Start a new section block. |
|
181
|
|
|
* @param string $name |
|
182
|
|
|
* @return null |
|
183
|
|
|
*/ |
|
184
|
12 |
|
public function start($name) |
|
185
|
|
|
{ |
|
186
|
12 |
|
if ($name === 'content') { |
|
187
|
4 |
|
throw new LogicException( |
|
188
|
1 |
|
'The section name "content" is reserved.' |
|
189
|
3 |
|
); |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
8 |
|
if (!isset($this->sections[$name])) { |
|
193
|
8 |
|
$this->sections[$name] = ''; |
|
194
|
6 |
|
} |
|
195
|
|
|
|
|
196
|
8 |
|
ob_start(); |
|
197
|
8 |
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* Stop the current section block. |
|
201
|
|
|
* @return null |
|
202
|
|
|
*/ |
|
203
|
12 |
|
public function stop() |
|
204
|
|
|
{ |
|
205
|
12 |
|
if (empty($this->sections)) { |
|
206
|
4 |
|
throw new LogicException( |
|
207
|
1 |
|
'You must start a section before you can stop it.' |
|
208
|
3 |
|
); |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
8 |
|
end($this->sections); |
|
212
|
|
|
|
|
213
|
8 |
|
$this->sections[key($this->sections)] = ob_get_clean(); |
|
214
|
8 |
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Stop the current section block and append the results. |
|
218
|
|
|
* @return null |
|
219
|
|
|
*/ |
|
220
|
8 |
|
public function append() |
|
221
|
|
|
{ |
|
222
|
8 |
|
if (empty($this->sections)) { |
|
223
|
4 |
|
throw new LogicException( |
|
224
|
1 |
|
'You must start a section before you can append it.' |
|
225
|
3 |
|
); |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
4 |
|
end($this->sections); |
|
229
|
|
|
|
|
230
|
4 |
|
$this->sections[key($this->sections)] .= ob_get_clean(); |
|
231
|
4 |
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* Returns the content for a section block. |
|
235
|
|
|
* @param string $name Section name |
|
236
|
|
|
* @param string $default Default section content |
|
237
|
|
|
* @return string|null |
|
238
|
|
|
*/ |
|
239
|
16 |
|
public function section($name, $default = null) |
|
240
|
|
|
{ |
|
241
|
16 |
|
if (!isset($this->sections[$name])) { |
|
242
|
8 |
|
return $default; |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
8 |
|
return $this->sections[$name]; |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* Fetch a rendered template. |
|
250
|
|
|
* @param string $name |
|
251
|
|
|
* @param array $data |
|
252
|
|
|
* @return string |
|
253
|
|
|
*/ |
|
254
|
4 |
|
public function fetch($name, array $data = array()) |
|
255
|
|
|
{ |
|
256
|
4 |
|
return $this->engine->render($name, $data); |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/** |
|
260
|
|
|
* Output a rendered template. |
|
261
|
|
|
* @param string $name |
|
262
|
|
|
* @param array $data |
|
263
|
|
|
* @return null |
|
264
|
|
|
*/ |
|
265
|
4 |
|
public function insert($name, array $data = array()) |
|
266
|
|
|
{ |
|
267
|
4 |
|
echo $this->engine->render($name, $data); |
|
268
|
4 |
|
} |
|
269
|
|
|
|
|
270
|
|
|
/** |
|
271
|
|
|
* Apply multiple functions to variable. |
|
272
|
|
|
* @param mixed $var |
|
273
|
|
|
* @param string $functions |
|
274
|
|
|
* @return mixed |
|
275
|
|
|
*/ |
|
276
|
12 |
|
public function batch($var, $functions) |
|
277
|
|
|
{ |
|
278
|
12 |
|
foreach (explode('|', $functions) as $function) { |
|
279
|
12 |
|
if ($this->engine->doesFunctionExist($function)) { |
|
280
|
4 |
|
$var = call_user_func(array($this, $function), $var); |
|
281
|
12 |
|
} elseif (is_callable($function)) { |
|
282
|
8 |
|
$var = call_user_func($function, $var); |
|
283
|
6 |
|
} else { |
|
284
|
4 |
|
throw new LogicException( |
|
285
|
6 |
|
'The batch function could not find the "' . $function . '" function.' |
|
286
|
3 |
|
); |
|
287
|
|
|
} |
|
288
|
6 |
|
} |
|
289
|
|
|
|
|
290
|
8 |
|
return $var; |
|
291
|
|
|
} |
|
292
|
|
|
|
|
293
|
|
|
/** |
|
294
|
|
|
* Escape string. |
|
295
|
|
|
* @param string $string |
|
296
|
|
|
* @param null|string $functions |
|
297
|
|
|
* @return string |
|
298
|
|
|
*/ |
|
299
|
12 |
|
public function escape($string, $functions = null) |
|
300
|
|
|
{ |
|
301
|
12 |
|
static $flags; |
|
302
|
|
|
|
|
303
|
12 |
|
if (!isset($flags)) { |
|
304
|
4 |
|
$flags = ENT_QUOTES | (defined('ENT_SUBSTITUTE') ? ENT_SUBSTITUTE : 0); |
|
305
|
3 |
|
} |
|
306
|
|
|
|
|
307
|
12 |
|
if ($functions) { |
|
308
|
4 |
|
$string = $this->batch($string, $functions); |
|
309
|
3 |
|
} |
|
310
|
|
|
|
|
311
|
12 |
|
return htmlspecialchars($string, $flags, 'UTF-8'); |
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
|
|
/** |
|
315
|
|
|
* Alias to escape function. |
|
316
|
|
|
* @param string $string |
|
317
|
|
|
* @param null|string $functions |
|
318
|
|
|
* @return string |
|
319
|
|
|
*/ |
|
320
|
4 |
|
public function e($string, $functions = null) |
|
321
|
|
|
{ |
|
322
|
4 |
|
return $this->escape($string, $functions); |
|
323
|
|
|
} |
|
324
|
|
|
} |
|
325
|
|
|
|