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 | 108 | 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 | 108 | public function data(array $data = null) |
|
99 | |||
100 | /** |
||
101 | * Check if the template exists. |
||
102 | * @return boolean |
||
103 | */ |
||
104 | 92 | public function exists() |
|
108 | |||
109 | /** |
||
110 | * Get the template path. |
||
111 | * @return string |
||
112 | */ |
||
113 | 88 | 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 | 84 | public function render(array $data = array()) |
|
126 | { |
||
127 | 84 | $this->data($data); |
|
128 | 84 | unset($data); |
|
129 | 84 | extract($this->data); |
|
130 | |||
131 | 84 | if (!$this->exists()) { |
|
132 | 4 | throw new LogicException( |
|
133 | 4 | 'The template "' . $this->name->getName() . '" could not be found at "' . $this->path() . '".' |
|
134 | 4 | ); |
|
135 | } |
||
136 | |||
137 | try { |
||
138 | 80 | $level = ob_get_level(); |
|
139 | 80 | ob_start(); |
|
140 | |||
141 | 80 | include $this->path(); |
|
142 | |||
143 | 64 | $content = ob_get_clean(); |
|
144 | |||
145 | 64 | if (isset($this->layoutName)) { |
|
146 | 12 | $layout = $this->engine->make($this->layoutName); |
|
147 | 12 | $layout->sections = array_merge($this->sections, array('content' => $content)); |
|
148 | 12 | $content = $layout->render($this->layoutData); |
|
149 | 12 | } |
|
150 | |||
151 | 64 | return $content; |
|
152 | 16 | } catch (Throwable $e) { |
|
|
|||
153 | while (ob_get_level() > $level) { |
||
154 | ob_end_clean(); |
||
155 | } |
||
156 | |||
157 | throw $e; |
||
158 | 16 | } catch (Exception $e) { |
|
159 | 16 | while (ob_get_level() > $level) { |
|
160 | 16 | ob_end_clean(); |
|
161 | 16 | } |
|
162 | |||
163 | 16 | 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 | 12 | public function layout($name, array $data = array()) |
|
178 | |||
179 | /** |
||
180 | * Start a new section block. |
||
181 | * @param string $name |
||
182 | * @return null |
||
183 | */ |
||
184 | 8 | public function start($name) |
|
185 | { |
||
186 | 8 | if ($name === 'content') { |
|
187 | 4 | throw new LogicException( |
|
188 | 'The section name "content" is reserved.' |
||
189 | 4 | ); |
|
190 | } |
||
191 | |||
192 | 4 | $this->sections[$name] = ''; |
|
193 | |||
194 | 4 | ob_start(); |
|
195 | 4 | } |
|
196 | |||
197 | /** |
||
198 | * Stop the current section block. |
||
199 | * @return null |
||
200 | */ |
||
201 | 8 | public function stop() |
|
202 | { |
||
203 | 8 | if (empty($this->sections)) { |
|
204 | 4 | throw new LogicException( |
|
205 | 'You must start a section before you can stop it.' |
||
206 | 4 | ); |
|
207 | } |
||
208 | |||
209 | 4 | end($this->sections); |
|
210 | |||
211 | 4 | $this->sections[key($this->sections)] = ob_get_clean(); |
|
212 | 4 | } |
|
213 | |||
214 | /** |
||
215 | * Returns the content for a section block. |
||
216 | * @param string $name Section name |
||
217 | * @param string $default Default section content |
||
218 | * @return string|null |
||
219 | */ |
||
220 | 12 | public function section($name, $default = null) |
|
228 | |||
229 | /** |
||
230 | * Fetch a rendered template. |
||
231 | * @param string $name |
||
232 | * @param array $data |
||
233 | * @return string |
||
234 | */ |
||
235 | 4 | public function fetch($name, array $data = array()) |
|
239 | |||
240 | /** |
||
241 | * Output a rendered template. |
||
242 | * @param string $name |
||
243 | * @param array $data |
||
244 | * @return null |
||
245 | */ |
||
246 | 4 | public function insert($name, array $data = array()) |
|
250 | |||
251 | /** |
||
252 | * Apply multiple functions to variable. |
||
253 | * @param mixed $var |
||
254 | * @param string $functions |
||
255 | * @return mixed |
||
256 | */ |
||
257 | 12 | public function batch($var, $functions) |
|
273 | |||
274 | /** |
||
275 | * Escape string. |
||
276 | * @param string $string |
||
277 | * @param null|string $functions |
||
278 | * @return string |
||
279 | */ |
||
280 | 12 | public function escape($string, $functions = null) |
|
294 | |||
295 | /** |
||
296 | * Alias to escape function. |
||
297 | * @param string $string |
||
298 | * @param null|string $functions |
||
299 | * @return string |
||
300 | */ |
||
301 | 4 | public function e($string, $functions = null) |
|
305 | } |
||
306 |