vendor/twig/twig/src/TemplateWrapper.php line 127

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of Twig.
  4. *
  5. * (c) Fabien Potencier
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Twig;
  11. use Twig\Error\RuntimeError;
  12. /**
  13. * Exposes a template to userland.
  14. *
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. */
  17. final class TemplateWrapper
  18. {
  19. /**
  20. * This method is for internal use only and should never be called
  21. * directly (use Twig\Environment::load() instead).
  22. *
  23. * @internal
  24. */
  25. public function __construct(
  26. private Environment $env,
  27. private Template $template,
  28. ) {
  29. }
  30. /**
  31. * @return iterable<scalar|\Stringable|null>
  32. */
  33. public function stream(array $context = []): iterable
  34. {
  35. yield from $this->template->yield($context);
  36. }
  37. /**
  38. * @return iterable<scalar|\Stringable|null>
  39. */
  40. public function streamBlock(string $name, array $context = []): iterable
  41. {
  42. yield from $this->template->yieldBlock($name, $context + $this->env->getGlobals());
  43. }
  44. public function render(array $context = []): string
  45. {
  46. return $this->template->render($context);
  47. }
  48. public function display(array $context = []): void
  49. {
  50. // using func_get_args() allows to not expose the blocks argument
  51. // as it should only be used by internal code
  52. $this->template->display($context, \func_get_args()[1] ?? []);
  53. }
  54. public function hasBlock(string $name, array $context = []): bool
  55. {
  56. return $this->template->hasBlock($name, $context + $this->env->getGlobals());
  57. }
  58. /**
  59. * @return string[] An array of defined template block names
  60. */
  61. public function getBlockNames(array $context = []): array
  62. {
  63. return $this->template->getBlockNames($context + $this->env->getGlobals());
  64. }
  65. public function renderBlock(string $name, array $context = []): string
  66. {
  67. return $this->template->renderBlock($name, $context + $this->env->getGlobals());
  68. }
  69. public function displayBlock(string $name, array $context = []): void
  70. {
  71. $context += $this->env->getGlobals();
  72. foreach ($this->template->yieldBlock($name, $context) as $data) {
  73. echo $data;
  74. }
  75. }
  76. public function getSourceContext(): Source
  77. {
  78. return $this->template->getSourceContext();
  79. }
  80. public function getTemplateName(): string
  81. {
  82. return $this->template->getTemplateName();
  83. }
  84. /**
  85. * Returns the escaping strategy the template body was compiled with.
  86. *
  87. * This describes the template's own source, not its output: `autoescape`,
  88. * `escape`, and anything rendered by a parent, embedded, or included
  89. * template can use another strategy.
  90. *
  91. * @return string|false The strategy name or false when the template is not autoescaped
  92. */
  93. public function getDefaultEscapeStrategy(): string|false
  94. {
  95. return $this->template->getDefaultEscapeStrategy();
  96. }
  97. /**
  98. * @internal
  99. */
  100. public function isOwnedBy(Environment $env): bool
  101. {
  102. return $this->env === $env && $this->template->isOwnedBy($env);
  103. }
  104. /**
  105. * @internal
  106. */
  107. public function unwrap(?Environment $env = null): Template
  108. {
  109. if (null === $env) {
  110. trigger_deprecation('twig/twig', '3.30', 'Calling "%s()" without arguments is deprecated, pass the Twig environment instead.', __METHOD__);
  111. } elseif (!$this->isOwnedBy($env)) {
  112. throw new RuntimeError(\sprintf('A "%s" can only be used with the "%s" that created it.', self::class, Environment::class));
  113. }
  114. return $this->template;
  115. }
  116. }