4 * This file is part of Twig.
9 * For the full copyright and license information, please view the LICENSE
10 * file that was distributed with this source code.
14 * Represents an include node.
16 * @author Fabien Potencier <fabien@symfony.com>
18 class Twig_Node_Include extends Twig_Node implements Twig_NodeOutputInterface
20 public function __construct(Twig_Node_Expression $expr, Twig_Node_Expression $variables = null, $only = false, $ignoreMissing = false, $lineno, $tag = null)
22 $nodes = array('expr' => $expr);
23 if (null !== $variables) {
24 $nodes['variables'] = $variables;
27 parent::__construct($nodes, array('only' => (bool) $only, 'ignore_missing' => (bool) $ignoreMissing), $lineno, $tag);
30 public function compile(Twig_Compiler $compiler)
32 $compiler->addDebugInfo($this);
34 if ($this->getAttribute('ignore_missing')) {
41 $this->addGetTemplate($compiler);
43 $compiler->raw('->display(');
45 $this->addTemplateArguments($compiler);
47 $compiler->raw(");\n");
49 if ($this->getAttribute('ignore_missing')) {
52 ->write("} catch (Twig_Error_Loader \$e) {\n")
54 ->write("// ignore missing template\n")
61 protected function addGetTemplate(Twig_Compiler $compiler)
64 ->write('$this->loadTemplate(')
65 ->subcompile($this->getNode('expr'))
67 ->repr($this->getTemplateName())
69 ->repr($this->getTemplateLine())
74 protected function addTemplateArguments(Twig_Compiler $compiler)
76 if (!$this->hasNode('variables')) {
77 $compiler->raw(false === $this->getAttribute('only') ? '$context' : 'array()');
78 } elseif (false === $this->getAttribute('only')) {
80 ->raw('array_merge($context, ')
81 ->subcompile($this->getNode('variables'))
85 $compiler->subcompile($this->getNode('variables'));
90 class_alias('Twig_Node_Include', 'Twig\Node\IncludeNode', false);