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
|
104 |
|
public function __construct(Engine $engine, $name) |
57
|
|
|
{ |
58
|
104 |
|
$this->engine = $engine; |
59
|
104 |
|
$this->name = new Name($engine, $name); |
60
|
|
|
|
61
|
104 |
|
$this->data($this->engine->getData($name)); |
62
|
104 |
|
} |
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 data to template object. |
88
|
|
|
* @param array $data |
89
|
|
|
* @return null |
90
|
|
|
*/ |
91
|
104 |
|
public function data(array $data) |
92
|
|
|
{ |
93
|
104 |
|
$this->data = array_merge($this->data, $data); |
94
|
104 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Check if the template exists. |
98
|
|
|
* @return boolean |
99
|
|
|
*/ |
100
|
92 |
|
public function exists() |
101
|
|
|
{ |
102
|
92 |
|
return $this->name->doesPathExist(); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get the template path. |
107
|
|
|
* @return string |
108
|
|
|
*/ |
109
|
88 |
|
public function path() |
110
|
|
|
{ |
111
|
88 |
|
return $this->name->getPath(); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Render the template and layout. |
116
|
|
|
* @param array $data |
117
|
|
|
* @throws \Throwable |
118
|
|
|
* @throws \Exception |
119
|
|
|
* @return string |
120
|
|
|
*/ |
121
|
84 |
|
public function render(array $data = array()) |
122
|
|
|
{ |
123
|
|
|
try { |
124
|
84 |
|
$this->data($data); |
125
|
|
|
|
126
|
84 |
|
unset($data); |
127
|
|
|
|
128
|
84 |
|
extract($this->data); |
129
|
|
|
|
130
|
84 |
|
ob_start(); |
131
|
|
|
|
132
|
84 |
|
if (!$this->exists()) { |
133
|
4 |
|
throw new LogicException( |
134
|
4 |
|
'The template "' . $this->name->getName() . '" could not be found at "' . $this->path() . '".' |
135
|
4 |
|
); |
136
|
|
|
} |
137
|
|
|
|
138
|
80 |
|
include $this->path(); |
139
|
|
|
|
140
|
64 |
|
$content = ob_get_clean(); |
141
|
|
|
|
142
|
64 |
|
if (isset($this->layoutName)) { |
143
|
12 |
|
$layout = $this->engine->make($this->layoutName); |
144
|
12 |
|
$layout->sections = array_merge($this->sections, array('content' => $content)); |
145
|
12 |
|
$content = $layout->render($this->layoutData); |
146
|
12 |
|
} |
147
|
|
|
|
148
|
64 |
|
return $content; |
149
|
20 |
|
} catch (Throwable $e) { |
|
|
|
|
150
|
|
|
ob_end_clean(); |
151
|
|
|
throw $e; |
152
|
20 |
|
} catch (Exception $e) { |
153
|
20 |
|
ob_end_clean(); |
154
|
20 |
|
throw $e; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Set the template's layout. |
160
|
|
|
* @param string $name |
161
|
|
|
* @param array $data |
162
|
|
|
* @return null |
163
|
|
|
*/ |
164
|
12 |
|
public function layout($name, array $data = array()) |
165
|
|
|
{ |
166
|
12 |
|
$this->layoutName = $name; |
167
|
12 |
|
$this->layoutData = $data; |
168
|
12 |
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Start a new section block. |
172
|
|
|
* @param string $name |
173
|
|
|
* @return null |
174
|
|
|
*/ |
175
|
8 |
|
public function start($name) |
176
|
|
|
{ |
177
|
8 |
|
if ($name === 'content') { |
178
|
4 |
|
throw new LogicException( |
179
|
|
|
'The section name "content" is reserved.' |
180
|
4 |
|
); |
181
|
|
|
} |
182
|
|
|
|
183
|
4 |
|
$this->sections[$name] = ''; |
184
|
|
|
|
185
|
4 |
|
ob_start(); |
186
|
4 |
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Stop the current section block. |
190
|
|
|
* @return null |
191
|
|
|
*/ |
192
|
8 |
|
public function stop() |
193
|
|
|
{ |
194
|
8 |
|
if (empty($this->sections)) { |
195
|
4 |
|
throw new LogicException( |
196
|
|
|
'You must start a section before you can stop it.' |
197
|
4 |
|
); |
198
|
|
|
} |
199
|
|
|
|
200
|
4 |
|
end($this->sections); |
201
|
|
|
|
202
|
4 |
|
$this->sections[key($this->sections)] = ob_get_clean(); |
203
|
4 |
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Returns the content for a section block. |
207
|
|
|
* @param string $name Section name |
208
|
|
|
* @param string $default Default section content |
209
|
|
|
* @return string|null |
210
|
|
|
*/ |
211
|
12 |
|
public function section($name, $default = null) |
212
|
|
|
{ |
213
|
12 |
|
if (!isset($this->sections[$name])) { |
214
|
8 |
|
return $default; |
215
|
|
|
} |
216
|
|
|
|
217
|
4 |
|
return $this->sections[$name]; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Fetch a rendered template. |
222
|
|
|
* @param string $name |
223
|
|
|
* @param array $data |
224
|
|
|
* @return string |
225
|
|
|
*/ |
226
|
4 |
|
public function fetch($name, array $data = array()) |
227
|
|
|
{ |
228
|
4 |
|
return $this->engine->render($name, $data); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Output a rendered template. |
233
|
|
|
* @param string $name |
234
|
|
|
* @param array $data |
235
|
|
|
* @return null |
236
|
|
|
*/ |
237
|
4 |
|
public function insert($name, array $data = array()) |
238
|
|
|
{ |
239
|
4 |
|
echo $this->engine->render($name, $data); |
240
|
4 |
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Apply multiple functions to variable. |
244
|
|
|
* @param mixed $var |
245
|
|
|
* @param string $functions |
246
|
|
|
* @return mixed |
247
|
|
|
*/ |
248
|
12 |
|
public function batch($var, $functions) |
249
|
|
|
{ |
250
|
12 |
|
foreach (explode('|', $functions) as $function) { |
251
|
12 |
|
if ($this->engine->doesFunctionExist($function)) { |
252
|
4 |
|
$var = call_user_func(array($this, $function), $var); |
253
|
12 |
|
} elseif (is_callable($function)) { |
254
|
8 |
|
$var = call_user_func($function, $var); |
255
|
8 |
|
} else { |
256
|
4 |
|
throw new LogicException( |
257
|
4 |
|
'The batch function could not find the "' . $function . '" function.' |
258
|
4 |
|
); |
259
|
|
|
} |
260
|
8 |
|
} |
261
|
|
|
|
262
|
8 |
|
return $var; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Escape string. |
267
|
|
|
* @param string $string |
268
|
|
|
* @param null|string $functions |
269
|
|
|
* @return string |
270
|
|
|
*/ |
271
|
12 |
|
public function escape($string, $functions = null) |
272
|
|
|
{ |
273
|
12 |
|
static $flags; |
274
|
|
|
|
275
|
12 |
|
if (!isset($flags)) { |
276
|
4 |
|
$flags = ENT_QUOTES | (defined('ENT_SUBSTITUTE') ? ENT_SUBSTITUTE : 0); |
277
|
4 |
|
} |
278
|
|
|
|
279
|
12 |
|
if ($functions) { |
280
|
4 |
|
$string = $this->batch($string, $functions); |
281
|
4 |
|
} |
282
|
|
|
|
283
|
12 |
|
return htmlspecialchars($string, $flags, 'UTF-8'); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
/** |
287
|
|
|
* Alias to escape function. |
288
|
|
|
* @param string $string |
289
|
|
|
* @param null|string $functions |
290
|
|
|
* @return string |
291
|
|
|
*/ |
292
|
4 |
|
public function e($string, $functions = null) |
293
|
|
|
{ |
294
|
4 |
|
return $this->escape($string, $functions); |
295
|
|
|
} |
296
|
|
|
} |
297
|
|
|
|