ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
Include.php
Go to the documentation of this file.
1<?php
2
3/*
4 * This file is part of Twig.
5 *
6 * (c) Fabien Potencier
7 * (c) 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
19{
20 public function __construct(Twig_Node_Expression $expr, Twig_Node_Expression $variables = null, $only = false, $ignoreMissing = false, $lineno, $tag = null)
21 {
22 $nodes = array('expr' => $expr);
23 if (null !== $variables) {
24 $nodes['variables'] = $variables;
25 }
26
27 parent::__construct($nodes, array('only' => (bool) $only, 'ignore_missing' => (bool) $ignoreMissing), $lineno, $tag);
28 }
29
30 public function compile(Twig_Compiler $compiler)
31 {
32 $compiler->addDebugInfo($this);
33
34 if ($this->getAttribute('ignore_missing')) {
35 $compiler
36 ->write("try {\n")
37 ->indent()
38 ;
39 }
40
41 $this->addGetTemplate($compiler);
42
43 $compiler->raw('->display(');
44
45 $this->addTemplateArguments($compiler);
46
47 $compiler->raw(");\n");
48
49 if ($this->getAttribute('ignore_missing')) {
50 $compiler
51 ->outdent()
52 ->write("} catch (Twig_Error_Loader \$e) {\n")
53 ->indent()
54 ->write("// ignore missing template\n")
55 ->outdent()
56 ->write("}\n\n")
57 ;
58 }
59 }
60
61 protected function addGetTemplate(Twig_Compiler $compiler)
62 {
63 $compiler
64 ->write('$this->loadTemplate(')
65 ->subcompile($this->getNode('expr'))
66 ->raw(', ')
67 ->repr($this->getTemplateName())
68 ->raw(', ')
69 ->repr($this->getTemplateLine())
70 ->raw(')')
71 ;
72 }
73
74 protected function addTemplateArguments(Twig_Compiler $compiler)
75 {
76 if (!$this->hasNode('variables')) {
77 $compiler->raw(false === $this->getAttribute('only') ? '$context' : 'array()');
78 } elseif (false === $this->getAttribute('only')) {
79 $compiler
80 ->raw('array_merge($context, ')
81 ->subcompile($this->getNode('variables'))
82 ->raw(')')
83 ;
84 } else {
85 $compiler->subcompile($this->getNode('variables'));
86 }
87 }
88}
89
90class_alias('Twig_Node_Include', 'Twig\Node\IncludeNode', false);
An exception for terminatinating execution or to throw for unit testing.
Compiles a node to PHP code.
Definition: Compiler.php:19
raw($string)
Adds a raw string to the compiled code.
Definition: Compiler.php:112
addDebugInfo(Twig_NodeInterface $node)
Adds debugging information.
Definition: Compiler.php:212
outdent($step=1)
Outdents the generated code.
Definition: Compiler.php:267
subcompile(Twig_NodeInterface $node, $raw=true)
Definition: Compiler.php:94
write()
Writes a string to the compiled code by adding indentation.
Definition: Compiler.php:124
Abstract class for all nodes that represents an expression.
Definition: Expression.php:19
Represents an include node.
Definition: Include.php:19
__construct(Twig_Node_Expression $expr, Twig_Node_Expression $variables=null, $only=false, $ignoreMissing=false, $lineno, $tag=null)
Definition: Include.php:20
compile(Twig_Compiler $compiler)
Compiles the node to PHP.
Definition: Include.php:30
addGetTemplate(Twig_Compiler $compiler)
Definition: Include.php:61
addTemplateArguments(Twig_Compiler $compiler)
Definition: Include.php:74
Represents a node in the AST.
Definition: Node.php:19
hasNode($name)
Definition: Node.php:178
getAttribute($name)
Definition: Node.php:152
getTemplateName()
Definition: Node.php:229
$lineno
Definition: Node.php:22
getTemplateLine()
Definition: Node.php:121
getNode($name)
Definition: Node.php:186
$nodes
Definition: Node.php:20
Represents a displayable node in the AST.