4 * This file is part of Twig.
6 * (c) 2009 Fabien Potencier
7 * (c) 2009 Armin Ronacher
9 * For the full copyright and license information, please view the LICENSE
10 * file that was distributed with this source code.
14 * Represents a module node.
16 * @author Fabien Potencier <fabien@symfony.com>
18 class Twig_Node_Module
extends Twig_Node
20 public function __construct(Twig_NodeInterface
$body, Twig_Node_Expression
$parent = null, Twig_NodeInterface
$blocks, Twig_NodeInterface
$macros, Twig_NodeInterface
$traits, $embeddedTemplates, $filename)
22 // embedded templates are set as attributes so that they are only visited once by the visitors
23 parent
::__construct(array('parent' => $parent, 'body' => $body, 'blocks' => $blocks, 'macros' => $macros, 'traits' => $traits), array('filename' => $filename, 'index' => null, 'embedded_templates' => $embeddedTemplates), 1);
26 public function setIndex($index)
28 $this->setAttribute('index', $index);
32 * Compiles the node to PHP.
34 * @param Twig_Compiler A Twig_Compiler instance
36 public function compile(Twig_Compiler
$compiler)
38 $this->compileTemplate($compiler);
40 foreach ($this->getAttribute('embedded_templates') as $template) {
41 $compiler->subcompile($template);
45 protected function compileTemplate(Twig_Compiler
$compiler)
47 if (!$this->getAttribute('index')) {
48 $compiler->write('<?php');
51 $this->compileClassHeader($compiler);
53 if (count($this->getNode('blocks')) || count($this->getNode('traits')) || null === $this->getNode('parent') || $this->getNode('parent') instanceof Twig_Node_Expression_Constant
) {
54 $this->compileConstructor($compiler);
57 $this->compileGetParent($compiler);
59 $this->compileDisplayHeader($compiler);
61 $this->compileDisplayBody($compiler);
63 $this->compileDisplayFooter($compiler);
65 $compiler->subcompile($this->getNode('blocks'));
67 $this->compileMacros($compiler);
69 $this->compileGetTemplateName($compiler);
71 $this->compileIsTraitable($compiler);
73 $this->compileDebugInfo($compiler);
75 $this->compileClassFooter($compiler);
78 protected function compileGetParent(Twig_Compiler
$compiler)
80 if (null === $this->getNode('parent')) {
85 ->write("protected function doGetParent(array \$context)\n", "{\n")
90 if ($this->getNode('parent') instanceof Twig_Node_Expression_Constant
) {
91 $compiler->subcompile($this->getNode('parent'));
94 ->raw("\$this->env->resolveTemplate(")
95 ->subcompile($this->getNode('parent'))
107 protected function compileDisplayBody(Twig_Compiler
$compiler)
109 $compiler->subcompile($this->getNode('body'));
111 if (null !== $this->getNode('parent')) {
112 if ($this->getNode('parent') instanceof Twig_Node_Expression_Constant
) {
113 $compiler->write("\$this->parent");
115 $compiler->write("\$this->getParent(\$context)");
117 $compiler->raw("->display(\$context, array_merge(\$this->blocks, \$blocks));\n");
121 protected function compileClassHeader(Twig_Compiler
$compiler)
125 // if the filename contains */, add a blank to avoid a PHP parse error
126 ->write("/* ".str_replace('*/', '* /', $this->getAttribute('filename'))." */\n")
127 ->write('class '.$compiler->getEnvironment()->getTemplateClass($this->getAttribute('filename'), $this->getAttribute('index')))
128 ->raw(sprintf(" extends %s\n", $compiler->getEnvironment()->getBaseTemplateClass()))
134 protected function compileConstructor(Twig_Compiler
$compiler)
137 ->write("public function __construct(Twig_Environment \$env)\n", "{\n")
139 ->write("parent::__construct(\$env);\n\n")
143 if (null === $this->getNode('parent')) {
144 $compiler->write("\$this->parent = false;\n\n");
145 } elseif ($this->getNode('parent') instanceof Twig_Node_Expression_Constant
) {
147 ->write("\$this->parent = \$this->env->loadTemplate(")
148 ->subcompile($this->getNode('parent'))
153 $countTraits = count($this->getNode('traits'));
156 foreach ($this->getNode('traits') as $i => $trait) {
157 $this->compileLoadTemplate($compiler, $trait->getNode('template'), sprintf('$_trait_%s', $i));
160 ->addDebugInfo($trait->getNode('template'))
161 ->write(sprintf("if (!\$_trait_%s->isTraitable()) {\n", $i))
163 ->write("throw new Twig_Error_Runtime('Template \"'.")
164 ->subcompile($trait->getNode('template'))
165 ->raw(".'\" cannot be used as a trait.');\n")
168 ->write(sprintf("\$_trait_%s_blocks = \$_trait_%s->getBlocks();\n\n", $i, $i))
171 foreach ($trait->getNode('targets') as $key => $value) {
173 ->write(sprintf("\$_trait_%s_blocks[", $i))
175 ->raw(sprintf("] = \$_trait_%s_blocks[", $i))
177 ->raw(sprintf("]; unset(\$_trait_%s_blocks[", $i))
184 if ($countTraits > 1) {
186 ->write("\$this->traits = array_merge(\n")
190 for ($i = 0; $i < $countTraits; $i++
) {
192 ->write(sprintf("\$_trait_%s_blocks".($i == $countTraits - 1 ? '' : ',')."\n", $i))
202 ->write("\$this->traits = \$_trait_0_blocks;\n\n")
207 ->write("\$this->blocks = array_merge(\n")
209 ->write("\$this->traits,\n")
214 ->write("\$this->blocks = array(\n")
223 foreach ($this->getNode('blocks') as $name => $node) {
225 ->write(sprintf("'%s' => array(\$this, 'block_%s'),\n", $name, $name))
244 protected function compileDisplayHeader(Twig_Compiler
$compiler)
247 ->write("protected function doDisplay(array \$context, array \$blocks = array())\n", "{\n")
252 protected function compileDisplayFooter(Twig_Compiler
$compiler)
260 protected function compileClassFooter(Twig_Compiler
$compiler)
268 protected function compileMacros(Twig_Compiler
$compiler)
270 $compiler->subcompile($this->getNode('macros'));
273 protected function compileGetTemplateName(Twig_Compiler
$compiler)
276 ->write("public function getTemplateName()\n", "{\n")
279 ->repr($this->getAttribute('filename'))
286 protected function compileIsTraitable(Twig_Compiler
$compiler)
288 // A template can be used as a trait if:
289 // * it has no parent
290 // * it has no macros
293 // Put another way, a template can be used as a trait if it
294 // only contains blocks and use statements.
295 $traitable = null === $this->getNode('parent') && 0 === count($this->getNode('macros'));
297 if ($this->getNode('body') instanceof Twig_Node_Body
) {
298 $nodes = $this->getNode('body')->getNode(0);
300 $nodes = $this->getNode('body');
303 if (!count($nodes)) {
304 $nodes = new Twig_Node(array($nodes));
307 foreach ($nodes as $node) {
312 if ($node instanceof Twig_Node_Text
&& ctype_space($node->getAttribute('data'))) {
316 if ($node instanceof Twig_Node_BlockReference
) {
330 ->write("public function isTraitable()\n", "{\n")
332 ->write(sprintf("return %s;\n", $traitable ? 'true' : 'false'))
338 protected function compileDebugInfo(Twig_Compiler
$compiler)
341 ->write("public function getDebugInfo()\n", "{\n")
343 ->write(sprintf("return %s;\n", str_replace("\n", '', var_export(array_reverse($compiler->getDebugInfo(), true), true))))
349 protected function compileLoadTemplate(Twig_Compiler
$compiler, $node, $var)
351 if ($node instanceof Twig_Node_Expression_Constant
) {
353 ->write(sprintf("%s = \$this->env->loadTemplate(", $var))
359 ->write(sprintf("%s = ", $var))
362 ->write(sprintf("if (!%s", $var))
363 ->raw(" instanceof Twig_Template) {\n")
365 ->write(sprintf("%s = \$this->env->loadTemplate(%s);\n", $var, $var))