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

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. public function render(array $context = []): string
  32. {
  33. // using func_get_args() allows to not expose the blocks argument
  34. // as it should only be used by internal code
  35. return $this->template->render($context, \func_get_args()[1] ?? []);
  36. }
  37. public function display(array $context = [])
  38. {
  39. // using func_get_args() allows to not expose the blocks argument
  40. // as it should only be used by internal code
  41. $this->template->display($context, \func_get_args()[1] ?? []);
  42. }
  43. public function hasBlock(string $name, array $context = []): bool
  44. {
  45. return $this->template->hasBlock($name, $context);
  46. }
  47. /**
  48. * @return string[] An array of defined template block names
  49. */
  50. public function getBlockNames(array $context = []): array
  51. {
  52. return $this->template->getBlockNames($context);
  53. }
  54. public function renderBlock(string $name, array $context = []): string
  55. {
  56. $context = $this->env->mergeGlobals($context);
  57. $level = ob_get_level();
  58. if ($this->env->isDebug()) {
  59. ob_start();
  60. } else {
  61. ob_start(function () { return ''; });
  62. }
  63. try {
  64. $this->template->displayBlock($name, $context);
  65. } catch (\Throwable $e) {
  66. while (ob_get_level() > $level) {
  67. ob_end_clean();
  68. }
  69. throw $e;
  70. }
  71. return ob_get_clean();
  72. }
  73. public function displayBlock(string $name, array $context = [])
  74. {
  75. $this->template->displayBlock($name, $this->env->mergeGlobals($context));
  76. }
  77. public function getSourceContext(): Source
  78. {
  79. return $this->template->getSourceContext();
  80. }
  81. public function getTemplateName(): string
  82. {
  83. return $this->template->getTemplateName();
  84. }
  85. /**
  86. * @internal
  87. *
  88. * @return Template
  89. */
  90. public function unwrap()
  91. {
  92. return $this->template;
  93. }
  94. }