1<?php 2 3/* 4 * This file is part of Twig. 5 * 6 * (c) Fabien Potencier 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12namespace Twig; 13 14/** 15 * Exposes a template to userland. 16 * 17 * @author Fabien Potencier <fabien@symfony.com> 18 */ 19final class TemplateWrapper 20{ 21 /** 22 * This method is for internal use only and should never be called 23 * directly (use Twig\Environment::load() instead). 24 * 25 * @internal 26 */ 27 public function __construct( 28 private Environment $env, 29 private Template $template, 30 ) { 31 } 32 33 /** 34 * @return iterable<scalar|\Stringable|null> 35 */ 36 public function stream(array $context = []): iterable 37 { 38 yield from $this->template->yield($context); 39 } 40 41 /** 42 * @return iterable<scalar|\Stringable|null> 43 */ 44 public function streamBlock(string $name, array $context = []): iterable 45 { 46 yield from $this->template->yieldBlock($name, $context); 47 } 48 49 public function render(array $context = []): string 50 { 51 return $this->template->render($context); 52 } 53 54 /** 55 * @return void 56 */ 57 public function display(array $context = []) 58 { 59 // using func_get_args() allows to not expose the blocks argument 60 // as it should only be used by internal code 61 $this->template->display($context, \func_get_args()[1] ?? []); 62 } 63 64 public function hasBlock(string $name, array $context = []): bool 65 { 66 return $this->template->hasBlock($name, $context); 67 } 68 69 /** 70 * @return string[] An array of defined template block names 71 */ 72 public function getBlockNames(array $context = []): array 73 { 74 return $this->template->getBlockNames($context); 75 } 76 77 public function renderBlock(string $name, array $context = []): string 78 { 79 return $this->template->renderBlock($name, $context + $this->env->getGlobals()); 80 } 81 82 /** 83 * @return void 84 */ 85 public function displayBlock(string $name, array $context = []) 86 { 87 $context += $this->env->getGlobals(); 88 foreach ($this->template->yieldBlock($name, $context) as $data) { 89 echo $data; 90 } 91 } 92 93 public function getSourceContext(): Source 94 { 95 return $this->template->getSourceContext(); 96 } 97 98 public function getTemplateName(): string 99 { 100 return $this->template->getTemplateName(); 101 } 102 103 /** 104 * @internal 105 * 106 * @return Template 107 */ 108 public function unwrap() 109 { 110 return $this->template; 111 } 112} 113