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.     /**
  48.      * @return void
  49.      */
  50.     public function display(array $context = [])
  51.     {
  52.         // using func_get_args() allows to not expose the blocks argument
  53.         // as it should only be used by internal code
  54.         $this->template->display($context\func_get_args()[1] ?? []);
  55.     }
  56.     public function hasBlock(string $name, array $context = []): bool
  57.     {
  58.         return $this->template->hasBlock($name$context);
  59.     }
  60.     /**
  61.      * @return string[] An array of defined template block names
  62.      */
  63.     public function getBlockNames(array $context = []): array
  64.     {
  65.         return $this->template->getBlockNames($context);
  66.     }
  67.     public function renderBlock(string $name, array $context = []): string
  68.     {
  69.         return $this->template->renderBlock($name$context $this->env->getGlobals());
  70.     }
  71.     /**
  72.      * @return void
  73.      */
  74.     public function displayBlock(string $name, array $context = [])
  75.     {
  76.         $context += $this->env->getGlobals();
  77.         foreach ($this->template->yieldBlock($name$context) as $data) {
  78.             echo $data;
  79.         }
  80.     }
  81.     public function getSourceContext(): Source
  82.     {
  83.         return $this->template->getSourceContext();
  84.     }
  85.     public function getTemplateName(): string
  86.     {
  87.         return $this->template->getTemplateName();
  88.     }
  89.     /**
  90.      * @internal
  91.      *
  92.      * @return Template
  93.      */
  94.     public function unwrap()
  95.     {
  96.         return $this->template;
  97.     }
  98. }