-
Notifications
You must be signed in to change notification settings - Fork 178
/
Copy pathTemplate.php
352 lines (304 loc) · 8.16 KB
/
Template.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
<?php
namespace League\Plates\Template;
use Exception;
use League\Plates\Engine;
use League\Plates\Exception\TemplateNotFound;
use LogicException;
use Throwable;
/**
* Container which holds template data and provides access to template functions.
*/
class Template
{
const SECTION_MODE_REWRITE = 1;
const SECTION_MODE_PREPEND = 2;
const SECTION_MODE_APPEND = 3;
/**
* Set section content mode: rewrite/append/prepend
* @var int
*/
protected $sectionMode = self::SECTION_MODE_REWRITE;
/**
* Instance of the template engine.
* @var Engine
*/
protected $engine;
/**
* The name of the template.
* @var Name
*/
protected $name;
/**
* The data assigned to the template.
* @var array
*/
protected $data = array();
/**
* An array of section content.
* @var array
*/
protected $sections = array();
/**
* The name of the section currently being rendered.
* @var string
*/
protected $sectionName;
/**
* Whether the section should be appended or not.
* @deprecated stayed for backward compatibility, use $sectionMode instead
* @var boolean
*/
protected $appendSection;
/**
* The name of the template layout.
* @var string
*/
protected $layoutName;
/**
* The data assigned to the template layout.
* @var array
*/
protected $layoutData;
/**
* Create new Template instance.
* @param Engine $engine
* @param string $name
*/
public function __construct(Engine $engine, $name)
{
$this->engine = $engine;
$this->name = new Name($engine, $name);
$this->data($this->engine->getData($name));
}
/**
* Magic method used to call extension functions.
* @param string $name
* @param array $arguments
* @return mixed
*/
public function __call($name, $arguments)
{
return $this->engine->getFunction($name)->call($this, $arguments);
}
/**
* Alias for render() method.
* @throws \Throwable
* @throws \Exception
* @return string
*/
public function __toString()
{
return $this->render();
}
/**
* Assign or get template data.
* @param array $data
* @return mixed
*/
public function data(array $data = null)
{
if (is_null($data)) {
return $this->data;
}
$this->data = array_merge($this->data, $data);
}
/**
* Check if the template exists.
* @return boolean
*/
public function exists()
{
try {
($this->engine->getResolveTemplatePath())($this->name);
return true;
} catch (TemplateNotFound $e) {
return false;
}
}
/**
* Get the template path.
* @return string
*/
public function path()
{
try {
return ($this->engine->getResolveTemplatePath())($this->name);
} catch (TemplateNotFound $e) {
return $e->paths()[0];
}
}
/**
* Render the template and layout.
* @param array $data
* @throws \Throwable
* @throws \Exception
* @return string
*/
public function render(array $data = array())
{
$this->data($data);
$path = ($this->engine->getResolveTemplatePath())($this->name);
try {
$level = ob_get_level();
ob_start();
(function() {
extract($this->data);
include func_get_arg(0);
})($path);
$content = ob_get_clean();
if (isset($this->layoutName)) {
$layout = $this->engine->make($this->layoutName);
$layout->sections = array_merge($this->sections, array('content' => $content));
$content = $layout->render($this->layoutData);
}
return $content;
} catch (Throwable $e) {
while (ob_get_level() > $level) {
ob_end_clean();
}
throw $e;
}
}
/**
* Set the template's layout.
* @param string $name
* @param array $data
* @return null
*/
public function layout($name, array $data = array())
{
$this->layoutName = $name;
$this->layoutData = $data;
}
/**
* Start a new section block.
* @param string $name
* @return null
*/
public function start($name)
{
if ($name === 'content') {
throw new LogicException(
'The section name "content" is reserved.'
);
}
if ($this->sectionName) {
throw new LogicException('You cannot nest sections within other sections.');
}
$this->sectionName = $name;
ob_start();
}
/**
* Start a new section block in APPEND mode.
* @param string $name
* @return null
*/
public function push($name)
{
$this->appendSection = true; /* for backward compatibility */
$this->sectionMode = self::SECTION_MODE_APPEND;
$this->start($name);
}
/**
* Start a new section block in PREPEND mode.
* @param string $name
* @return null
*/
public function unshift($name)
{
$this->appendSection = false; /* for backward compatibility */
$this->sectionMode = self::SECTION_MODE_PREPEND;
$this->start($name);
}
/**
* Stop the current section block.
* @return null
*/
public function stop()
{
if (is_null($this->sectionName)) {
throw new LogicException(
'You must start a section before you can stop it.'
);
}
if (!isset($this->sections[$this->sectionName])) {
$this->sections[$this->sectionName] = '';
}
switch ($this->sectionMode) {
case self::SECTION_MODE_REWRITE:
$this->sections[$this->sectionName] = ob_get_clean();
break;
case self::SECTION_MODE_APPEND:
$this->sections[$this->sectionName] .= ob_get_clean();
break;
case self::SECTION_MODE_PREPEND:
$this->sections[$this->sectionName] = ob_get_clean().$this->sections[$this->sectionName];
break;
}
$this->sectionName = null;
$this->sectionMode = self::SECTION_MODE_REWRITE;
$this->appendSection = false; /* for backward compatibility */
}
/**
* Alias of stop().
* @return null
*/
public function end()
{
$this->stop();
}
/**
* Returns the content for a section block.
* @param string $name Section name
* @param string $default Default section content
* @return string|null
*/
public function section($name, $default = null)
{
if (!isset($this->sections[$name])) {
return $default;
}
return $this->sections[$name];
}
/**
* Fetch a rendered template.
* @param string $name
* @param array $data
* @return string
*/
public function fetch($name, array $data = array())
{
return $this->engine->render($name, $data);
}
/**
* Output a rendered template.
* @param string $name
* @param array $data
* @return null
*/
public function insert($name, array $data = array())
{
echo $this->engine->render($name, $data);
}
/**
* Apply multiple functions to variable.
* @param mixed $var
* @param string $functions
* @return mixed
*/
public function batch($var, $functions)
{
foreach (explode('|', $functions) as $function) {
if ($this->engine->doesFunctionExist($function)) {
$var = call_user_func(array($this, $function), $var);
} elseif (is_callable($function)) {
$var = call_user_func($function, $var);
} else {
throw new LogicException(
'The batch function could not find the "' . $function . '" function.'
);
}
}
return $var;
}
}