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

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.     /**
  30.      * @return iterable<scalar|\Stringable|null>
  31.      */
  32.     public function stream(array $context = []): iterable
  33.     {
  34.         yield from $this->template->yield($context);
  35.     }
  36.     /**
  37.      * @return iterable<scalar|\Stringable|null>
  38.      */
  39.     public function streamBlock(string $name, array $context = []): iterable
  40.     {
  41.         yield from $this->template->yieldBlock($name$context);
  42.     }
  43.     public function render(array $context = []): string
  44.     {
  45.         return $this->template->render($context);
  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.     public function hasBlock(string $name, array $context = []): bool
  54.     {
  55.         return $this->template->hasBlock($name$context);
  56.     }
  57.     /**
  58.      * @return string[] An array of defined template block names
  59.      */
  60.     public function getBlockNames(array $context = []): array
  61.     {
  62.         return $this->template->getBlockNames($context);
  63.     }
  64.     public function renderBlock(string $name, array $context = []): string
  65.     {
  66.         return $this->template->renderBlock($name$context $this->env->getGlobals());
  67.     }
  68.     public function displayBlock(string $name, array $context = [])
  69.     {
  70.         $context += $this->env->getGlobals();
  71.         foreach ($this->template->yieldBlock($name$context) as $data) {
  72.             echo $data;
  73.         }
  74.     }
  75.     public function getSourceContext(): Source
  76.     {
  77.         return $this->template->getSourceContext();
  78.     }
  79.     public function getTemplateName(): string
  80.     {
  81.         return $this->template->getTemplateName();
  82.     }
  83.     /**
  84.      * @internal
  85.      *
  86.      * @return Template
  87.      */
  88.     public function unwrap()
  89.     {
  90.         return $this->template;
  91.     }
  92. }