ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
Twig_Node_Macro Class Reference

Represents a macro node. More...

+ Inheritance diagram for Twig_Node_Macro:
+ Collaboration diagram for Twig_Node_Macro:

Public Member Functions

 __construct ($name, Twig_NodeInterface $body, Twig_NodeInterface $arguments, $lineno, $tag=null)
 
 compile (Twig_Compiler $compiler)
 Compiles the node to PHP. More...
 
- Public Member Functions inherited from Twig_Node
 __construct (array $nodes=array(), array $attributes=array(), $lineno=0, $tag=null)
 Constructor. More...
 
 __toString ()
 
 toXml ($asDom=false)
 
 compile (Twig_Compiler $compiler)
 Compiles the node to PHP. More...
 
 getTemplateLine ()
 
 getLine ()
 
 getNodeTag ()
 
 hasAttribute ($name)
 
 getAttribute ($name)
 
 setAttribute ($name, $value)
 
 removeAttribute ($name)
 
 hasNode ($name)
 
 getNode ($name)
 
 setNode ($name, $node=null)
 
 removeNode ($name)
 
 count ()
 
 getIterator ()
 
 setTemplateName ($name)
 
 getTemplateName ()
 
 setFilename ($name)
 
 getFilename ()
 
 compile (Twig_Compiler $compiler)
 Compiles the node to PHP. More...
 
 getLine ()
 
 getNodeTag ()
 

Data Fields

const VARARGS_NAME = 'varargs'
 

Additional Inherited Members

- Protected Attributes inherited from Twig_Node
 $nodes
 
 $attributes
 
 $lineno
 
 $tag
 

Detailed Description

Represents a macro node.

Author
Fabien Potencier fabie.nosp@m.n@sy.nosp@m.mfony.nosp@m..com

Definition at line 17 of file Macro.php.

Constructor & Destructor Documentation

◆ __construct()

Twig_Node_Macro::__construct (   $name,
Twig_NodeInterface  $body,
Twig_NodeInterface  $arguments,
  $lineno,
  $tag = null 
)

Definition at line 21 of file Macro.php.

22 {
23 foreach ($arguments as $argumentName => $argument) {
24 if (self::VARARGS_NAME === $argumentName) {
25 throw new Twig_Error_Syntax(sprintf('The argument "%s" in macro "%s" cannot be defined because the variable "%s" is reserved for arbitrary arguments.', self::VARARGS_NAME, $name, self::VARARGS_NAME), $argument->getTemplateLine());
26 }
27 }
28
29 parent::__construct(array('body' => $body, 'arguments' => $arguments), array('name' => $name), $lineno, $tag);
30 }
sprintf('%.4f', $callTime)
Exception thrown when a syntax error occurs during lexing or parsing of a template.
Definition: Syntax.php:19
$lineno
Definition: Node.php:22

References Twig_Node\$lineno, Twig_Node\$name, Twig_Node\$tag, and sprintf.

Member Function Documentation

◆ compile()

Twig_Node_Macro::compile ( Twig_Compiler  $compiler)

Compiles the node to PHP.

Reimplemented from Twig_Node.

Definition at line 32 of file Macro.php.

33 {
34 $compiler
35 ->addDebugInfo($this)
36 ->write(sprintf('public function get%s(', $this->getAttribute('name')))
37 ;
38
39 $count = count($this->getNode('arguments'));
40 $pos = 0;
41 foreach ($this->getNode('arguments') as $name => $default) {
42 $compiler
43 ->raw('$__'.$name.'__ = ')
44 ->subcompile($default)
45 ;
46
47 if (++$pos < $count) {
48 $compiler->raw(', ');
49 }
50 }
51
52 if (PHP_VERSION_ID >= 50600) {
53 if ($count) {
54 $compiler->raw(', ');
55 }
56
57 $compiler->raw('...$__varargs__');
58 }
59
60 $compiler
61 ->raw(")\n")
62 ->write("{\n")
63 ->indent()
64 ;
65
66 $compiler
67 ->write("\$context = \$this->env->mergeGlobals(array(\n")
68 ->indent()
69 ;
70
71 foreach ($this->getNode('arguments') as $name => $default) {
72 $compiler
73 ->write('')
74 ->string($name)
75 ->raw(' => $__'.$name.'__')
76 ->raw(",\n")
77 ;
78 }
79
80 $compiler
81 ->write('')
82 ->string(self::VARARGS_NAME)
83 ->raw(' => ')
84 ;
85
86 if (PHP_VERSION_ID >= 50600) {
87 $compiler->raw("\$__varargs__,\n");
88 } else {
89 $compiler
90 ->raw('func_num_args() > ')
91 ->repr($count)
92 ->raw(' ? array_slice(func_get_args(), ')
93 ->repr($count)
94 ->raw(") : array(),\n")
95 ;
96 }
97
98 $compiler
99 ->outdent()
100 ->write("));\n\n")
101 ->write("\$blocks = array();\n\n")
102 ->write("ob_start();\n")
103 ->write("try {\n")
104 ->indent()
105 ->subcompile($this->getNode('body'))
106 ->outdent()
107 ->write("} catch (Exception \$e) {\n")
108 ->indent()
109 ->write("ob_end_clean();\n\n")
110 ->write("throw \$e;\n")
111 ->outdent()
112 ->write("} catch (Throwable \$e) {\n")
113 ->indent()
114 ->write("ob_end_clean();\n\n")
115 ->write("throw \$e;\n")
116 ->outdent()
117 ->write("}\n\n")
118 ->write("return ('' === \$tmp = ob_get_clean()) ? '' : new Twig_Markup(\$tmp, \$this->env->getCharset());\n")
119 ->outdent()
120 ->write("}\n\n")
121 ;
122 }
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
write()
Writes a string to the compiled code by adding indentation.
Definition: Compiler.php:124
getAttribute($name)
Definition: Node.php:152
getNode($name)
Definition: Node.php:186
count()
Definition: Node.php:209

References Twig_Node\$name, Twig_Compiler\addDebugInfo(), Twig_Node\count(), Twig_Node\getAttribute(), Twig_Node\getNode(), Twig_Compiler\outdent(), Twig_Compiler\raw(), sprintf, and Twig_Compiler\write().

+ Here is the call graph for this function:

Field Documentation

◆ VARARGS_NAME

const Twig_Node_Macro::VARARGS_NAME = 'varargs'

Definition at line 19 of file Macro.php.


The documentation for this class was generated from the following file: