29 @trigger_error(sprintf(
'Passing a string as the $name argument of %s() is deprecated since version 1.27. Pass a Twig_Source instance instead.', __METHOD__), E_USER_DEPRECATED);
32 $this->source =
$name;
46 if (null !== $parent) {
47 $nodes[
'parent'] = $parent;
51 parent::__construct(
$nodes, array(
53 'source' => $this->source->getCode(),
55 'filename' => $this->source->getName(),
57 'embedded_templates' => $embeddedTemplates,
81 $compiler->
write(
'<?php');
120 if (!$this->
hasNode(
'parent')) {
123 $parent = $this->
getNode(
'parent');
126 ->
write(
"protected function doGetParent(array \$context)\n",
"{\n")
128 ->addDebugInfo($parent)
136 ->
raw(
'$this->loadTemplate(')
137 ->subcompile($parent)
139 ->repr($this->source->getName())
141 ->repr($parent->getTemplateLine())
158 ->write(
'/* '.str_replace(
'*/',
'* /', $this->source->getName()).
" */\n")
160 ->raw(sprintf(
" extends %s\n", $compiler->
getEnvironment()->getBaseTemplateClass()))
169 ->
write(
"public function __construct(Twig_Environment \$env)\n",
"{\n")
171 ->subcompile($this->
getNode(
'constructor_start'))
172 ->write(
"parent::__construct(\$env);\n\n")
176 if (!$this->
hasNode(
'parent')) {
177 $compiler->
write(
"\$this->parent = false;\n\n");
181 ->write(
'$this->parent = $this->loadTemplate(')
182 ->subcompile($parent)
184 ->repr($this->source->getName())
186 ->repr($parent->getTemplateLine())
194 foreach ($this->
getNode(
'traits') as
$i => $trait) {
199 ->
write(sprintf(
"if (!\$_trait_%s->isTraitable()) {\n",
$i))
201 ->write(
"throw new Twig_Error_Runtime('Template \"'.")
202 ->subcompile($trait->getNode(
'template'))
203 ->raw(
".'\" cannot be used as a trait.');\n")
206 ->write(sprintf(
"\$_trait_%s_blocks = \$_trait_%s->getBlocks();\n\n",
$i,
$i))
209 foreach ($trait->getNode(
'targets') as
$key => $value) {
211 ->
write(sprintf(
'if (!isset($_trait_%s_blocks[',
$i))
215 ->write(
"throw new Twig_Error_Runtime(sprintf('Block ")
217 ->raw(
' is not defined in trait ')
218 ->subcompile($trait->getNode(
'template'))
223 ->write(sprintf(
'$_trait_%s_blocks[',
$i))
225 ->raw(sprintf(
'] = $_trait_%s_blocks[',
$i))
227 ->raw(sprintf(
']; unset($_trait_%s_blocks[',
$i))
234 if ($countTraits > 1) {
236 ->
write(
"\$this->traits = array_merge(\n")
240 for (
$i = 0;
$i < $countTraits; ++
$i) {
242 ->
write(sprintf(
'$_trait_%s_blocks'.(
$i == $countTraits - 1 ?
'' :
',').
"\n",
$i))
252 ->
write(
"\$this->traits = \$_trait_0_blocks;\n\n")
257 ->
write(
"\$this->blocks = array_merge(\n")
259 ->write(
"\$this->traits,\n")
264 ->
write(
"\$this->blocks = array(\n")
290 ->subcompile($this->
getNode(
'constructor_end'))
298 ->
write(
"protected function doDisplay(array \$context, array \$blocks = array())\n",
"{\n")
300 ->subcompile($this->
getNode(
'display_start'))
301 ->subcompile($this->
getNode(
'body'))
304 if ($this->
hasNode(
'parent')) {
305 $parent = $this->
getNode(
'parent');
308 $compiler->
write(
'$this->parent');
310 $compiler->
write(
'$this->getParent($context)');
312 $compiler->
raw(
"->display(\$context, array_merge(\$this->blocks, \$blocks));\n");
339 ->
write(
"public function getTemplateName()\n",
"{\n")
342 ->repr($this->source->getName())
370 foreach (
$nodes as $node) {
375 if ($node instanceof
Twig_Node_Text && ctype_space($node->getAttribute(
'data'))) {
393 ->
write(
"public function isTraitable()\n",
"{\n")
395 ->write(sprintf(
"return %s;\n", $traitable ?
'true' :
'false'))
404 ->
write(
"public function getDebugInfo()\n",
"{\n")
406 ->write(sprintf(
"return %s;\n", str_replace(
"\n",
'', var_export(array_reverse($compiler->
getDebugInfo(),
true),
true))))
415 ->
write(
"/** @deprecated since 1.27 (to be removed in 2.0). Use getSourceContext() instead */\n")
416 ->write(
"public function getSource()\n",
"{\n")
418 ->write(
"@trigger_error('The '.__METHOD__.' method is deprecated since version 1.27 and will be removed in 2.0. Use getSourceContext() instead.', E_USER_DEPRECATED);\n\n")
419 ->write(
'return $this->getSourceContext()->getCode();')
429 ->
write(
"public function getSourceContext()\n",
"{\n")
431 ->write(
'return new Twig_Source(')
432 ->string($compiler->
getEnvironment()->isDebug() ? $this->source->getCode() :
'')
434 ->string($this->source->getName())
436 ->string($this->source->getPath())
447 ->
write(sprintf(
'%s = $this->loadTemplate(', $var))
450 ->repr($node->getTemplateName())
452 ->repr($node->getTemplateLine())
456 throw new LogicException(
'Trait templates can only be constant nodes.');
461 class_alias(
'Twig_Node_Module',
'Twig\Node\ModuleNode',
false);
raw($string)
Adds a raw string to the compiled code.
Represents a node in the AST.
compileGetSourceContext(Twig_Compiler $compiler)
outdent($step=1)
Outdents the generated code.
subcompile(Twig_NodeInterface $node, $raw=true)
compileGetSource(Twig_Compiler $compiler)
Represents a node in the AST.
compileClassHeader(Twig_Compiler $compiler)
compileGetTemplateName(Twig_Compiler $compiler)
indent($step=1)
Indents the generated code.
compileConstructor(Twig_Compiler $compiler)
__construct(Twig_NodeInterface $body, Twig_Node_Expression $parent=null, Twig_NodeInterface $blocks, Twig_NodeInterface $macros, Twig_NodeInterface $traits, $embeddedTemplates, $name, $source='')
compileDebugInfo(Twig_Compiler $compiler)
Represents a block call node.
Abstract class for all nodes that represents an expression.
compileIsTraitable(Twig_Compiler $compiler)
Represents a module node.
getEnvironment()
Returns the environment instance related to this compiler.
compileMacros(Twig_Compiler $compiler)
setAttribute($name, $value)
compile(Twig_Compiler $compiler)
Compiles the node to PHP.
compileGetParent(Twig_Compiler $compiler)
Holds information about a non-compiled Twig template.
compileTemplate(Twig_Compiler $compiler)
addDebugInfo(Twig_NodeInterface $node)
Adds debugging information.
compileClassFooter(Twig_Compiler $compiler)
compileLoadTemplate(Twig_Compiler $compiler, $node, $var)
write()
Writes a string to the compiled code by adding indentation.
compileDisplay(Twig_Compiler $compiler)