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

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. return $this->template->render($context);
  34. }
  35. public function display(array $context = [])
  36. {
  37. // using func_get_args() allows to not expose the blocks argument
  38. // as it should only be used by internal code
  39. $this->template->display($context, \func_get_args()[1] ?? []);
  40. }
  41. public function hasBlock(string $name, array $context = []): bool
  42. {
  43. return $this->template->hasBlock($name, $context);
  44. }
  45. /**
  46. * @return string[] An array of defined template block names
  47. */
  48. public function getBlockNames(array $context = []): array
  49. {
  50. return $this->template->getBlockNames($context);
  51. }
  52. public function renderBlock(string $name, array $context = []): string
  53. {
  54. return $this->template->renderBlock($name, $this->env->mergeGlobals($context));
  55. }
  56. public function displayBlock(string $name, array $context = [])
  57. {
  58. $context = $this->env->mergeGlobals($context);
  59. foreach ($this->template->yieldBlock($name, $context) as $data) {
  60. echo $data;
  61. }
  62. }
  63. public function getSourceContext(): Source
  64. {
  65. return $this->template->getSourceContext();
  66. }
  67. public function getTemplateName(): string
  68. {
  69. return $this->template->getTemplateName();
  70. }
  71. /**
  72. * @internal
  73. *
  74. * @return Template
  75. */
  76. public function unwrap()
  77. {
  78. return $this->template;
  79. }
  80. }