vendor/twig/twig/src/Node/Expression/BlockReferenceExpression.php line 25

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) Fabien Potencier
  6. * (c) Armin Ronacher
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Twig\Node\Expression;
  12. use Twig\Compiler;
  13. use Twig\Node\Node;
  14. /**
  15. * Represents a block call node.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. */
  19. class BlockReferenceExpression extends AbstractExpression
  20. {
  21. public function __construct(Node $name, ?Node $template, int $lineno, string $tag = null)
  22. {
  23. $nodes = ['name' => $name];
  24. if (null !== $template) {
  25. $nodes['template'] = $template;
  26. }
  27. parent::__construct($nodes, ['is_defined_test' => false, 'output' => false], $lineno, $tag);
  28. }
  29. public function compile(Compiler $compiler)
  30. {
  31. if ($this->getAttribute('is_defined_test')) {
  32. $this->compileTemplateCall($compiler, 'hasBlock');
  33. } else {
  34. if ($this->getAttribute('output')) {
  35. $compiler->addDebugInfo($this);
  36. $this
  37. ->compileTemplateCall($compiler, 'displayBlock')
  38. ->raw(";\n");
  39. } else {
  40. $this->compileTemplateCall($compiler, 'renderBlock');
  41. }
  42. }
  43. }
  44. private function compileTemplateCall(Compiler $compiler, string $method): Compiler
  45. {
  46. if (!$this->hasNode('template')) {
  47. $compiler->write('$this');
  48. } else {
  49. $compiler
  50. ->write('$this->loadTemplate(')
  51. ->subcompile($this->getNode('template'))
  52. ->raw(', ')
  53. ->repr($this->getTemplateName())
  54. ->raw(', ')
  55. ->repr($this->getTemplateLine())
  56. ->raw(')')
  57. ;
  58. }
  59. $compiler->raw(sprintf('->%s', $method));
  60. return $this->compileBlockArguments($compiler);
  61. }
  62. private function compileBlockArguments(Compiler $compiler): Compiler
  63. {
  64. $compiler
  65. ->raw('(')
  66. ->subcompile($this->getNode('name'))
  67. ->raw(', $context');
  68. if (!$this->hasNode('template')) {
  69. $compiler->raw(', $blocks');
  70. }
  71. return $compiler->raw(')');
  72. }
  73. }
  74. class_alias('Twig\Node\Expression\BlockReferenceExpression', 'Twig_Node_Expression_BlockReference');