1 | <?php |
||
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 | 120 | public function __construct(Engine $engine, $name) |
|
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) |
|
74 | |||
75 | /** |
||
76 | * Alias for render() method. |
||
77 | * @throws \Throwable |
||
78 | * @throws \Exception |
||
79 | * @return string |
||
80 | */ |
||
81 | 4 | public function __toString() |
|
85 | |||
86 | /** |
||
87 | * Assign or get template data. |
||
88 | * @param array $data |
||
89 | * @return mixed |
||
90 | */ |
||
91 | 120 | public function data(array $data = null) |
|
99 | |||
100 | /** |
||
101 | * Check if the template exists. |
||
102 | * @return boolean |
||
103 | */ |
||
104 | 104 | public function exists() |
|
108 | |||
109 | /** |
||
110 | * Get the template path. |
||
111 | * @return string |
||
112 | */ |
||
113 | 100 | public function path() |
|
117 | |||
118 | /** |
||
119 | * Render the template and layout. |
||
120 | * @param array $data |
||
121 | * @throws \Throwable |
||
122 | * @throws \Exception |
||
123 | * @return string |
||
124 | */ |
||
125 | 96 | public function render(array $data = array()) |
|
166 | |||
167 | /** |
||
168 | * Set the template's layout. |
||
169 | * @param string $name |
||
170 | * @param array $data |
||
171 | * @return null |
||
172 | */ |
||
173 | 20 | public function layout($name, array $data = array()) |
|
178 | |||
179 | /** |
||
180 | * Start a new section block. |
||
181 | * @param string $name |
||
182 | * @return null |
||
183 | */ |
||
184 | 16 | public function start($name) |
|
185 | { |
||
186 | 16 | if ($name === 'content') { |
|
187 | 4 | throw new LogicException( |
|
188 | 1 | 'The section name "content" is reserved.' |
|
189 | 3 | ); |
|
190 | } |
||
191 | |||
192 | 12 | if (isset($this->sections[$name])) { |
|
193 | 8 | $content = $this->sections[$name]; |
|
194 | 8 | unset($this->sections[$name]); |
|
195 | 8 | $this->sections[$name] = $content; |
|
196 | 6 | } else { |
|
197 | 12 | $this->sections[$name] = ''; |
|
198 | } |
||
199 | |||
200 | 12 | ob_start(); |
|
201 | 12 | } |
|
202 | |||
203 | /** |
||
204 | * Stop the current section block. |
||
205 | * @return null |
||
206 | */ |
||
207 | 16 | public function stop() |
|
208 | { |
||
209 | 16 | if (empty($this->sections)) { |
|
210 | 4 | throw new LogicException( |
|
211 | 1 | 'You must start a section before you can stop it.' |
|
212 | 3 | ); |
|
213 | } |
||
214 | |||
215 | 12 | end($this->sections); |
|
216 | |||
217 | 12 | $this->sections[key($this->sections)] = ob_get_clean(); |
|
218 | 12 | } |
|
219 | |||
220 | /** |
||
221 | * Stop the current section block and append the results. |
||
222 | * @return null |
||
223 | */ |
||
224 | 12 | public function append() |
|
225 | { |
||
226 | 12 | if (empty($this->sections)) { |
|
227 | 4 | throw new LogicException( |
|
228 | 1 | 'You must start a section before you can append it.' |
|
229 | 3 | ); |
|
230 | } |
||
231 | |||
232 | 8 | end($this->sections); |
|
233 | |||
234 | 8 | $this->sections[key($this->sections)] .= ob_get_clean(); |
|
235 | 8 | } |
|
236 | |||
237 | /** |
||
238 | * Returns the content for a section block. |
||
239 | * @param string $name Section name |
||
240 | * @param string $default Default section content |
||
241 | * @return string|null |
||
242 | */ |
||
243 | 20 | public function section($name, $default = null) |
|
251 | |||
252 | /** |
||
253 | * Fetch a rendered template. |
||
254 | * @param string $name |
||
255 | * @param array $data |
||
256 | * @return string |
||
257 | */ |
||
258 | 4 | public function fetch($name, array $data = array()) |
|
262 | |||
263 | /** |
||
264 | * Output a rendered template. |
||
265 | * @param string $name |
||
266 | * @param array $data |
||
267 | * @return null |
||
268 | */ |
||
269 | 4 | public function insert($name, array $data = array()) |
|
273 | |||
274 | /** |
||
275 | * Apply multiple functions to variable. |
||
276 | * @param mixed $var |
||
277 | * @param string $functions |
||
278 | * @return mixed |
||
279 | */ |
||
280 | 12 | public function batch($var, $functions) |
|
296 | |||
297 | /** |
||
298 | * Escape string. |
||
299 | * @param string $string |
||
300 | * @param null|string $functions |
||
301 | * @return string |
||
302 | */ |
||
303 | 12 | public function escape($string, $functions = null) |
|
317 | |||
318 | /** |
||
319 | * Alias to escape function. |
||
320 | * @param string $string |
||
321 | * @param null|string $functions |
||
322 | * @return string |
||
323 | */ |
||
324 | 4 | public function e($string, $functions = null) |
|
328 | } |
||
329 |