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