class IncludeNode
Represents an include node.
@author Fabien Potencier <fabien@symfony.com>
Hierarchy
- class \Twig\Node\Node implements \Twig\Node\Countable, \Twig\Node\IteratorAggregate
- class \Twig\Node\IncludeNode extends \Twig\Node\Node implements \Twig\Node\NodeOutputInterface
Expanded class hierarchy of IncludeNode
3 files declare their use of IncludeNode
- IncludeTokenParser.php in vendor/
twig/ twig/ src/ TokenParser/ IncludeTokenParser.php - OptimizerNodeVisitor.php in vendor/
twig/ twig/ src/ NodeVisitor/ OptimizerNodeVisitor.php - SandboxTokenParser.php in vendor/
twig/ twig/ src/ TokenParser/ SandboxTokenParser.php
File
-
vendor/
twig/ twig/ src/ Node/ IncludeNode.php, line 24
Namespace
Twig\NodeView source
class IncludeNode extends Node implements NodeOutputInterface {
public function __construct(AbstractExpression $expr, ?AbstractExpression $variables, bool $only, bool $ignoreMissing, int $lineno) {
$nodes = [
'expr' => $expr,
];
if (null !== $variables) {
$nodes['variables'] = $variables;
}
parent::__construct($nodes, [
'only' => $only,
'ignore_missing' => $ignoreMissing,
], $lineno);
}
public function compile(Compiler $compiler) : void {
$compiler->addDebugInfo($this);
if ($this->getAttribute('ignore_missing')) {
$template = $compiler->getVarName();
$compiler->write("try {\n")
->indent()
->write(\sprintf('$%s = ', $template));
$this->addGetTemplate($compiler, $template);
$compiler->raw(";\n")
->outdent()
->write("} catch (LoaderError \$e) {\n")
->indent()
->write("// ignore missing template\n")
->write(\sprintf("\${$template} = null;\n", $template))
->outdent()
->write("}\n")
->write(\sprintf("if (\$%s) {\n", $template))
->indent()
->write(\sprintf('yield from $%s->unwrap()->yield(', $template));
$this->addTemplateArguments($compiler);
$compiler->raw(");\n")
->outdent()
->write("}\n");
}
else {
$compiler->write('yield from ');
$this->addGetTemplate($compiler);
$compiler->raw('->unwrap()->yield(');
$this->addTemplateArguments($compiler);
$compiler->raw(");\n");
}
}
protected function addGetTemplate(Compiler $compiler) {
$compiler->raw('$this->loadTemplate(')
->subcompile($this->getNode('expr'))
->raw(', ')
->repr($this->getTemplateName())
->raw(', ')
->repr($this->getTemplateLine())
->raw(')');
}
protected function addTemplateArguments(Compiler $compiler) {
if (!$this->hasNode('variables')) {
$compiler->raw(false === $this->getAttribute('only') ? '$context' : '[]');
}
elseif (false === $this->getAttribute('only')) {
$compiler->raw('CoreExtension::merge($context, ')
->subcompile($this->getNode('variables'))
->raw(')');
}
else {
$compiler->raw('CoreExtension::toArray(');
$compiler->subcompile($this->getNode('variables'));
$compiler->raw(')');
}
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|
IncludeNode::addGetTemplate | protected | function | 1 | ||
IncludeNode::addTemplateArguments | protected | function | |||
IncludeNode::compile | public | function | Overrides Node::compile | ||
IncludeNode::__construct | public | function | Overrides Node::__construct | 1 | |
Node::$attributeNameDeprecations | private | property | @var array<string, NameDeprecation> | ||
Node::$attributes | protected | property | |||
Node::$lineno | protected | property | |||
Node::$nodeNameDeprecations | private | property | @var array<string, NameDeprecation> | ||
Node::$nodes | protected | property | |||
Node::$sourceContext | private | property | |||
Node::$tag | protected | property | |||
Node::count | public | function | |||
Node::deprecateAttribute | public | function | |||
Node::deprecateNode | public | function | |||
Node::getAttribute | public | function | |||
Node::getIterator | public | function | |||
Node::getNode | public | function | |||
Node::getNodeTag | public | function | |||
Node::getSourceContext | public | function | |||
Node::getTemplateLine | public | function | |||
Node::getTemplateName | public | function | |||
Node::hasAttribute | public | function | |||
Node::hasNode | public | function | |||
Node::removeAttribute | public | function | |||
Node::removeNode | public | function | |||
Node::setAttribute | public | function | |||
Node::setNode | public | function | 1 | ||
Node::setNodeTag | public | function | @internal | ||
Node::setSourceContext | public | function | |||
Node::__toString | public | function |