class BlockReferenceExpression
Represents a block call node.
@author Fabien Potencier <fabien@symfony.com>
Hierarchy
- class \Twig\Node\Node implements \Twig\Node\Countable, \Twig\Node\IteratorAggregate
- class \Twig\Node\Expression\AbstractExpression extends \Twig\Node\Node
- class \Twig\Node\Expression\BlockReferenceExpression extends \Twig\Node\Expression\AbstractExpression
- class \Twig\Node\Expression\AbstractExpression extends \Twig\Node\Node
Expanded class hierarchy of BlockReferenceExpression
4 files declare their use of BlockReferenceExpression
- CoreExtension.php in vendor/
twig/ twig/ src/ Extension/ CoreExtension.php - DefinedTest.php in vendor/
twig/ twig/ src/ Node/ Expression/ Test/ DefinedTest.php - OptimizerNodeVisitor.php in vendor/
twig/ twig/ src/ NodeVisitor/ OptimizerNodeVisitor.php - SafeAnalysisNodeVisitor.php in vendor/
twig/ twig/ src/ NodeVisitor/ SafeAnalysisNodeVisitor.php
File
-
vendor/
twig/ twig/ src/ Node/ Expression/ BlockReferenceExpression.php, line 23
Namespace
Twig\Node\ExpressionView source
class BlockReferenceExpression extends AbstractExpression {
/**
* @param AbstractExpression $name
*/
public function __construct(Node $name, ?Node $template, int $lineno) {
if (!$name instanceof AbstractExpression) {
trigger_deprecation('twig/twig', '3.15', 'Not passing a "%s" instance to the "node" argument of "%s" is deprecated ("%s" given).', AbstractExpression::class, static::class, get_class($name));
}
$nodes = [
'name' => $name,
];
if (null !== $template) {
$nodes['template'] = $template;
}
parent::__construct($nodes, [
'is_defined_test' => false,
'output' => false,
], $lineno);
}
public function compile(Compiler $compiler) : void {
if ($this->getAttribute('is_defined_test')) {
$this->compileTemplateCall($compiler, 'hasBlock');
}
else {
if ($this->getAttribute('output')) {
$compiler->addDebugInfo($this);
$compiler->write('yield from ');
$this->compileTemplateCall($compiler, 'yieldBlock')
->raw(";\n");
}
else {
$this->compileTemplateCall($compiler, 'renderBlock');
}
}
}
private function compileTemplateCall(Compiler $compiler, string $method) : Compiler {
if (!$this->hasNode('template')) {
$compiler->write('$this');
}
else {
$compiler->write('$this->loadTemplate(')
->subcompile($this->getNode('template'))
->raw(', ')
->repr($this->getTemplateName())
->raw(', ')
->repr($this->getTemplateLine())
->raw(')');
}
$compiler->raw(\sprintf('->unwrap()->%s', $method));
return $this->compileBlockArguments($compiler);
}
private function compileBlockArguments(Compiler $compiler) : Compiler {
$compiler->raw('(')
->subcompile($this->getNode('name'))
->raw(', $context');
if (!$this->hasNode('template')) {
$compiler->raw(', $blocks');
}
return $compiler->raw(')');
}
}