|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of Twig. |
|
|
5 |
* |
|
|
6 |
* (c) 2009 Fabien Potencier |
|
|
7 |
* (c) 2009 Armin Ronacher |
|
|
8 |
* |
|
|
9 |
* For the full copyright and license information, please view the LICENSE |
|
|
10 |
* file that was distributed with this source code. |
|
|
11 |
*/ |
|
|
12 |
|
|
|
13 |
/** |
|
|
14 |
* Default base class for compiled templates. |
|
|
15 |
* |
|
|
16 |
* @package twig |
|
|
17 |
* @author Fabien Potencier <fabien@symfony.com> |
|
|
18 |
*/ |
|
|
19 |
abstract class Twig_Template implements Twig_TemplateInterface |
|
|
20 |
{ |
|
|
21 |
static protected $cache = array(); |
|
|
22 |
|
|
|
23 |
protected $env; |
|
|
24 |
protected $blocks; |
|
|
25 |
|
|
|
26 |
/** |
|
|
27 |
* Constructor. |
|
|
28 |
* |
|
|
29 |
* @param Twig_Environment $env A Twig_Environment instance |
|
|
30 |
*/ |
|
|
31 |
public function __construct(Twig_Environment $env) |
|
|
32 |
{ |
|
|
33 |
$this->env = $env; |
|
|
34 |
$this->blocks = array(); |
|
|
35 |
} |
|
|
36 |
|
|
|
37 |
/** |
|
|
38 |
* Returns the template name. |
|
|
39 |
* |
|
|
40 |
* @return string The template name |
|
|
41 |
*/ |
|
|
42 |
public function getTemplateName() |
|
|
43 |
{ |
|
|
44 |
return null; |
|
|
45 |
} |
|
|
46 |
|
|
|
47 |
/** |
|
|
48 |
* Returns the Twig environment. |
|
|
49 |
* |
|
|
50 |
* @return Twig_Environment The Twig environment |
|
|
51 |
*/ |
|
|
52 |
public function getEnvironment() |
|
|
53 |
{ |
|
|
54 |
return $this->env; |
|
|
55 |
} |
|
|
56 |
|
|
|
57 |
/** |
|
|
58 |
* Returns the parent template. |
|
|
59 |
* |
|
|
60 |
* @return Twig_TemplateInterface|false The parent template or false if there is no parent |
|
|
61 |
*/ |
|
|
62 |
public function getParent(array $context) |
|
|
63 |
{ |
|
|
64 |
return false; |
|
|
65 |
} |
|
|
66 |
|
|
|
67 |
/** |
|
|
68 |
* Displays a parent block. |
|
|
69 |
* |
|
|
70 |
* @param string $name The block name to display from the parent |
|
|
71 |
* @param array $context The context |
|
|
72 |
* @param array $blocks The current set of blocks |
|
|
73 |
*/ |
|
|
74 |
public function displayParentBlock($name, array $context, array $blocks = array()) |
|
|
75 |
{ |
|
|
76 |
if (false !== $parent = $this->getParent($context)) { |
|
|
77 |
$parent->displayBlock($name, $context, $blocks); |
|
|
78 |
} else { |
|
|
79 |
throw new Twig_Error_Runtime('This template has no parent', -1, $this->getTemplateName()); |
|
|
80 |
} |
|
|
81 |
} |
|
|
82 |
|
|
|
83 |
/** |
|
|
84 |
* Displays a block. |
|
|
85 |
* |
|
|
86 |
* @param string $name The block name to display |
|
|
87 |
* @param array $context The context |
|
|
88 |
* @param array $blocks The current set of blocks |
|
|
89 |
*/ |
|
|
90 |
public function displayBlock($name, array $context, array $blocks = array()) |
|
|
91 |
{ |
|
|
92 |
if (isset($blocks[$name])) { |
|
|
93 |
$b = $blocks; |
|
|
94 |
unset($b[$name]); |
|
|
95 |
call_user_func($blocks[$name], $context, $b); |
|
|
96 |
} elseif (isset($this->blocks[$name])) { |
|
|
97 |
call_user_func($this->blocks[$name], $context, $blocks); |
|
|
98 |
} elseif (false !== $parent = $this->getParent($context)) { |
|
|
99 |
$parent->displayBlock($name, $context, array_merge($this->blocks, $blocks)); |
|
|
100 |
} |
|
|
101 |
} |
|
|
102 |
|
|
|
103 |
/** |
|
|
104 |
* Renders a parent block. |
|
|
105 |
* |
|
|
106 |
* @param string $name The block name to render from the parent |
|
|
107 |
* @param array $context The context |
|
|
108 |
* @param array $blocks The current set of blocks |
|
|
109 |
* |
|
|
110 |
* @return string The rendered block |
|
|
111 |
*/ |
|
|
112 |
public function renderParentBlock($name, array $context, array $blocks = array()) |
|
|
113 |
{ |
|
|
114 |
ob_start(); |
|
|
115 |
$this->displayParentBlock($name, $context, $blocks); |
|
|
116 |
|
|
|
117 |
return ob_get_clean(); |
|
|
118 |
} |
|
|
119 |
|
|
|
120 |
/** |
|
|
121 |
* Renders a block. |
|
|
122 |
* |
|
|
123 |
* @param string $name The block name to render |
|
|
124 |
* @param array $context The context |
|
|
125 |
* @param array $blocks The current set of blocks |
|
|
126 |
* |
|
|
127 |
* @return string The rendered block |
|
|
128 |
*/ |
|
|
129 |
public function renderBlock($name, array $context, array $blocks = array()) |
|
|
130 |
{ |
|
|
131 |
ob_start(); |
|
|
132 |
$this->displayBlock($name, $context, $blocks); |
|
|
133 |
|
|
|
134 |
return ob_get_clean(); |
|
|
135 |
} |
|
|
136 |
|
|
|
137 |
/** |
|
|
138 |
* Returns whether a block exists or not. |
|
|
139 |
* |
|
|
140 |
* @param string $name The block name |
|
|
141 |
* |
|
|
142 |
* @return Boolean true if the block exists, false otherwise |
|
|
143 |
*/ |
|
|
144 |
public function hasBlock($name) |
|
|
145 |
{ |
|
|
146 |
return isset($this->blocks[$name]); |
|
|
147 |
} |
|
|
148 |
|
|
|
149 |
/** |
|
|
150 |
* Returns all block names. |
|
|
151 |
* |
|
|
152 |
* @return array An array of block names |
|
|
153 |
*/ |
|
|
154 |
public function getBlockNames() |
|
|
155 |
{ |
|
|
156 |
return array_keys($this->blocks); |
|
|
157 |
} |
|
|
158 |
|
|
|
159 |
/** |
|
|
160 |
* Returns all blocks. |
|
|
161 |
* |
|
|
162 |
* @return array An array of blocks |
|
|
163 |
*/ |
|
|
164 |
public function getBlocks() |
|
|
165 |
{ |
|
|
166 |
return $this->blocks; |
|
|
167 |
} |
|
|
168 |
|
|
|
169 |
/** |
|
|
170 |
* Displays the template with the given context. |
|
|
171 |
* |
|
|
172 |
* @param array $context An array of parameters to pass to the template |
|
|
173 |
* @param array $blocks An array of blocks to pass to the template |
|
|
174 |
*/ |
|
|
175 |
public function display(array $context, array $blocks = array()) |
|
|
176 |
{ |
|
|
177 |
try { |
|
|
178 |
$this->doDisplay($context, $blocks); |
|
|
179 |
} catch (Twig_Error $e) { |
|
|
180 |
throw $e; |
|
|
181 |
} catch (Exception $e) { |
|
|
182 |
throw new Twig_Error_Runtime(sprintf('An exception has been thrown during the rendering of a template ("%s").', $e->getMessage()), -1, null, $e); |
|
|
183 |
} |
|
|
184 |
} |
|
|
185 |
|
|
|
186 |
/** |
|
|
187 |
* Renders the template with the given context and returns it as string. |
|
|
188 |
* |
|
|
189 |
* @param array $context An array of parameters to pass to the template |
|
|
190 |
* |
|
|
191 |
* @return string The rendered template |
|
|
192 |
*/ |
|
|
193 |
public function render(array $context) |
|
|
194 |
{ |
|
|
195 |
$level = ob_get_level(); |
|
|
196 |
ob_start(); |
|
|
197 |
try { |
|
|
198 |
$this->display($context); |
|
|
199 |
} catch (Exception $e) { |
|
|
200 |
while (ob_get_level() > $level) { |
|
|
201 |
ob_end_clean(); |
|
|
202 |
} |
|
|
203 |
|
|
|
204 |
throw $e; |
|
|
205 |
} |
|
|
206 |
|
|
|
207 |
return ob_get_clean(); |
|
|
208 |
} |
|
|
209 |
|
|
|
210 |
/** |
|
|
211 |
* Auto-generated method to display the template with the given context. |
|
|
212 |
* |
|
|
213 |
* @param array $context An array of parameters to pass to the template |
|
|
214 |
* @param array $blocks An array of blocks to pass to the template |
|
|
215 |
*/ |
|
|
216 |
abstract protected function doDisplay(array $context, array $blocks = array()); |
|
|
217 |
|
|
|
218 |
/** |
|
|
219 |
* Returns a variable from the context. |
|
|
220 |
* |
|
|
221 |
* @param array $context The context |
|
|
222 |
* @param string $item The variable to return from the context |
|
|
223 |
* |
|
|
224 |
* @return The content of the context variable |
|
|
225 |
* |
|
|
226 |
* @throws Twig_Error_Runtime if the variable does not exist and Twig is running in strict mode |
|
|
227 |
*/ |
|
|
228 |
protected function getContext($context, $item) |
|
|
229 |
{ |
|
|
230 |
if (!array_key_exists($item, $context)) { |
|
|
231 |
if (!$this->env->isStrictVariables()) { |
|
|
232 |
return null; |
|
|
233 |
} |
|
|
234 |
|
|
|
235 |
throw new Twig_Error_Runtime(sprintf('Variable "%s" does not exist', $item)); |
|
|
236 |
} |
|
|
237 |
|
|
|
238 |
return $context[$item]; |
|
|
239 |
} |
|
|
240 |
|
|
|
241 |
/** |
|
|
242 |
* Returns the attribute value for a given array/object. |
|
|
243 |
* |
|
|
244 |
* @param mixed $object The object or array from where to get the item |
|
|
245 |
* @param mixed $item The item to get from the array or object |
|
|
246 |
* @param array $arguments An array of arguments to pass if the item is an object method |
|
|
247 |
* @param string $type The type of attribute (@see Twig_TemplateInterface) |
|
|
248 |
* @param Boolean $isDefinedTest Whether this is only a defined check |
|
|
249 |
*/ |
|
|
250 |
protected function getAttribute($object, $item, array $arguments = array(), $type = Twig_TemplateInterface::ANY_CALL, $isDefinedTest = false) |
|
|
251 |
{ |
|
|
252 |
// array |
|
|
253 |
if (Twig_TemplateInterface::METHOD_CALL !== $type) { |
|
|
254 |
if ((is_array($object) && array_key_exists($item, $object)) |
|
|
255 |
|| ($object instanceof ArrayAccess && isset($object[$item])) |
|
|
256 |
) { |
|
|
257 |
if ($isDefinedTest) { |
|
|
258 |
return true; |
|
|
259 |
} |
|
|
260 |
|
|
|
261 |
return $object[$item]; |
|
|
262 |
} |
|
|
263 |
|
|
|
264 |
if (Twig_TemplateInterface::ARRAY_CALL === $type) { |
|
|
265 |
if ($isDefinedTest) { |
|
|
266 |
return false; |
|
|
267 |
} |
|
|
268 |
|
|
|
269 |
if (!$this->env->isStrictVariables()) { |
|
|
270 |
return null; |
|
|
271 |
} |
|
|
272 |
|
|
|
273 |
if (is_object($object)) { |
|
|
274 |
throw new Twig_Error_Runtime(sprintf('Key "%s" in object (with ArrayAccess) of type "%s" does not exist', $item, get_class($object))); |
|
|
275 |
// array |
|
|
276 |
} else { |
|
|
277 |
throw new Twig_Error_Runtime(sprintf('Key "%s" for array with keys "%s" does not exist', $item, implode(', ', array_keys($object)))); |
|
|
278 |
} |
|
|
279 |
} |
|
|
280 |
} |
|
|
281 |
|
|
|
282 |
if (!is_object($object)) { |
|
|
283 |
if ($isDefinedTest) { |
|
|
284 |
return false; |
|
|
285 |
} |
|
|
286 |
|
|
|
287 |
if (!$this->env->isStrictVariables()) { |
|
|
288 |
return null; |
|
|
289 |
} |
|
|
290 |
|
|
|
291 |
throw new Twig_Error_Runtime(sprintf('Item "%s" for "%s" does not exist', $item, $object)); |
|
|
292 |
} |
|
|
293 |
|
|
|
294 |
// get some information about the object |
|
|
295 |
$class = get_class($object); |
|
|
296 |
if (!isset(self::$cache[$class])) { |
|
|
297 |
$r = new ReflectionClass($class); |
|
|
298 |
self::$cache[$class] = array('methods' => array(), 'properties' => array()); |
|
|
299 |
foreach ($r->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { |
|
|
300 |
self::$cache[$class]['methods'][strtolower($method->getName())] = true; |
|
|
301 |
} |
|
|
302 |
|
|
|
303 |
foreach ($r->getProperties(ReflectionProperty::IS_PUBLIC) as $property) { |
|
|
304 |
self::$cache[$class]['properties'][$property->getName()] = true; |
|
|
305 |
} |
|
|
306 |
} |
|
|
307 |
|
|
|
308 |
// object property |
|
|
309 |
if (Twig_TemplateInterface::METHOD_CALL !== $type) { |
|
|
310 |
if (isset(self::$cache[$class]['properties'][$item]) |
|
|
311 |
|| isset($object->$item) || array_key_exists($item, $object) |
|
|
312 |
) { |
|
|
313 |
if ($isDefinedTest) { |
|
|
314 |
return true; |
|
|
315 |
} |
|
|
316 |
|
|
|
317 |
if ($this->env->hasExtension('sandbox')) { |
|
|
318 |
$this->env->getExtension('sandbox')->checkPropertyAllowed($object, $item); |
|
|
319 |
} |
|
|
320 |
|
|
|
321 |
return $object->$item; |
|
|
322 |
} |
|
|
323 |
} |
|
|
324 |
|
|
|
325 |
// object method |
|
|
326 |
$lcItem = strtolower($item); |
|
|
327 |
if (isset(self::$cache[$class]['methods'][$lcItem])) { |
|
|
328 |
$method = $item; |
|
|
329 |
} elseif (isset(self::$cache[$class]['methods']['get'.$lcItem])) { |
|
|
330 |
$method = 'get'.$item; |
|
|
331 |
} elseif (isset(self::$cache[$class]['methods']['is'.$lcItem])) { |
|
|
332 |
$method = 'is'.$item; |
|
|
333 |
} elseif (isset(self::$cache[$class]['methods']['__call'])) { |
|
|
334 |
$method = $item; |
|
|
335 |
} else { |
|
|
336 |
if ($isDefinedTest) { |
|
|
337 |
return false; |
|
|
338 |
} |
|
|
339 |
|
|
|
340 |
if (!$this->env->isStrictVariables()) { |
|
|
341 |
return null; |
|
|
342 |
} |
|
|
343 |
|
|
|
344 |
throw new Twig_Error_Runtime(sprintf('Method "%s" for object "%s" does not exist', $item, get_class($object))); |
|
|
345 |
} |
|
|
346 |
|
|
|
347 |
if ($isDefinedTest) { |
|
|
348 |
return true; |
|
|
349 |
} |
|
|
350 |
|
|
|
351 |
if ($this->env->hasExtension('sandbox')) { |
|
|
352 |
$this->env->getExtension('sandbox')->checkMethodAllowed($object, $method); |
|
|
353 |
} |
|
|
354 |
|
|
|
355 |
$ret = call_user_func_array(array($object, $method), $arguments); |
|
|
356 |
|
|
|
357 |
if ($object instanceof Twig_TemplateInterface) { |
|
|
358 |
return new Twig_Markup($ret); |
|
|
359 |
} |
|
|
360 |
|
|
|
361 |
return $ret; |
|
|
362 |
} |
|
|
363 |
} |