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

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. /**
  19. * This method is for internal use only and should never be called
  20. * directly (use Twig\Environment::load() instead).
  21. *
  22. * @internal
  23. */
  24. public function __construct(
  25. private Environment $env,
  26. private Template $template,
  27. ) {
  28. }
  29. public function render(array $context = []): string
  30. {
  31. return $this->template->render($context);
  32. }
  33. public function display(array $context = [])
  34. {
  35. // using func_get_args() allows to not expose the blocks argument
  36. // as it should only be used by internal code
  37. $this->template->display($context, \func_get_args()[1] ?? []);
  38. }
  39. public function hasBlock(string $name, array $context = []): bool
  40. {
  41. return $this->template->hasBlock($name, $context);
  42. }
  43. /**
  44. * @return string[] An array of defined template block names
  45. */
  46. public function getBlockNames(array $context = []): array
  47. {
  48. return $this->template->getBlockNames($context);
  49. }
  50. public function renderBlock(string $name, array $context = []): string
  51. {
  52. return $this->template->renderBlock($name, $context + $this->env->getGlobals());
  53. }
  54. public function displayBlock(string $name, array $context = [])
  55. {
  56. $context += $this->env->getGlobals();
  57. foreach ($this->template->yieldBlock($name, $context) as $data) {
  58. echo $data;
  59. }
  60. }
  61. public function getSourceContext(): Source
  62. {
  63. return $this->template->getSourceContext();
  64. }
  65. public function getTemplateName(): string
  66. {
  67. return $this->template->getTemplateName();
  68. }
  69. /**
  70. * @internal
  71. *
  72. * @return Template
  73. */
  74. public function unwrap()
  75. {
  76. return $this->template;
  77. }
  78. }