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

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. /**
  12. * Exposes a template to userland.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. final class TemplateWrapper
  17. {
  18. private $env;
  19. private $template;
  20. /**
  21. * This method is for internal use only and should never be called
  22. * directly (use Twig\Environment::load() instead).
  23. *
  24. * @internal
  25. */
  26. public function __construct(Environment $env, Template $template)
  27. {
  28. $this->env = $env;
  29. $this->template = $template;
  30. }
  31. /**
  32. * Renders the template.
  33. *
  34. * @param array $context An array of parameters to pass to the template
  35. */
  36. public function render(array $context = []): string
  37. {
  38. // using func_get_args() allows to not expose the blocks argument
  39. // as it should only be used by internal code
  40. return $this->template->render($context, \func_get_args()[1] ?? []);
  41. }
  42. /**
  43. * Displays the template.
  44. *
  45. * @param array $context An array of parameters to pass to the template
  46. */
  47. public function display(array $context = [])
  48. {
  49. // using func_get_args() allows to not expose the blocks argument
  50. // as it should only be used by internal code
  51. $this->template->display($context, \func_get_args()[1] ?? []);
  52. }
  53. /**
  54. * Checks if a block is defined.
  55. *
  56. * @param string $name The block name
  57. * @param array $context An array of parameters to pass to the template
  58. */
  59. public function hasBlock(string $name, array $context = []): bool
  60. {
  61. return $this->template->hasBlock($name, $context);
  62. }
  63. /**
  64. * Returns defined block names in the template.
  65. *
  66. * @param array $context An array of parameters to pass to the template
  67. *
  68. * @return string[] An array of defined template block names
  69. */
  70. public function getBlockNames(array $context = []): array
  71. {
  72. return $this->template->getBlockNames($context);
  73. }
  74. /**
  75. * Renders a template block.
  76. *
  77. * @param string $name The block name to render
  78. * @param array $context An array of parameters to pass to the template
  79. *
  80. * @return string The rendered block
  81. */
  82. public function renderBlock(string $name, array $context = []): string
  83. {
  84. $context = $this->env->mergeGlobals($context);
  85. $level = ob_get_level();
  86. if ($this->env->isDebug()) {
  87. ob_start();
  88. } else {
  89. ob_start(function () { return ''; });
  90. }
  91. try {
  92. $this->template->displayBlock($name, $context);
  93. } catch (\Throwable $e) {
  94. while (ob_get_level() > $level) {
  95. ob_end_clean();
  96. }
  97. throw $e;
  98. }
  99. return ob_get_clean();
  100. }
  101. /**
  102. * Displays a template block.
  103. *
  104. * @param string $name The block name to render
  105. * @param array $context An array of parameters to pass to the template
  106. */
  107. public function displayBlock(string $name, array $context = [])
  108. {
  109. $this->template->displayBlock($name, $this->env->mergeGlobals($context));
  110. }
  111. public function getSourceContext(): Source
  112. {
  113. return $this->template->getSourceContext();
  114. }
  115. public function getTemplateName(): string
  116. {
  117. return $this->template->getTemplateName();
  118. }
  119. /**
  120. * @internal
  121. *
  122. * @return Template
  123. */
  124. public function unwrap()
  125. {
  126. return $this->template;
  127. }
  128. }
  129. class_alias('Twig\TemplateWrapper', 'Twig_TemplateWrapper');