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 private $env; 22 private $template; 23 24 /** 25 * This method is for internal use only and should never be called 26 * directly (use Twig\Environment::load() instead). 27 * 28 * @internal 29 */ 30 public function __construct(Environment $env, Template $template) 31 { 32 $this->env = $env; 33 $this->template = $template; 34 } 35 36 /** 37 * Renders the template. 38 * 39 * @param array $context An array of parameters to pass to the template 40 */ 41 public function render(array $context = []): string 42 { 43 // using func_get_args() allows to not expose the blocks argument 44 // as it should only be used by internal code 45 return $this->template->render($context, \func_get_args()[1] ?? []); 46 } 47 48 /** 49 * Displays the template. 50 * 51 * @param array $context An array of parameters to pass to the template 52 */ 53 public function display(array $context = []) 54 { 55 // using func_get_args() allows to not expose the blocks argument 56 // as it should only be used by internal code 57 $this->template->display($context, \func_get_args()[1] ?? []); 58 } 59 60 /** 61 * Checks if a block is defined. 62 * 63 * @param string $name The block name 64 * @param array $context An array of parameters to pass to the template 65 */ 66 public function hasBlock(string $name, array $context = []): bool 67 { 68 return $this->template->hasBlock($name, $context); 69 } 70 71 /** 72 * Returns defined block names in the template. 73 * 74 * @param array $context An array of parameters to pass to the template 75 * 76 * @return string[] An array of defined template block names 77 */ 78 public function getBlockNames(array $context = []): array 79 { 80 return $this->template->getBlockNames($context); 81 } 82 83 /** 84 * Renders a template block. 85 * 86 * @param string $name The block name to render 87 * @param array $context An array of parameters to pass to the template 88 * 89 * @return string The rendered block 90 */ 91 public function renderBlock(string $name, array $context = []): string 92 { 93 $context = $this->env->mergeGlobals($context); 94 $level = ob_get_level(); 95 if ($this->env->isDebug()) { 96 ob_start(); 97 } else { 98 ob_start(function () { return ''; }); 99 } 100 try { 101 $this->template->displayBlock($name, $context); 102 } catch (\Throwable $e) { 103 while (ob_get_level() > $level) { 104 ob_end_clean(); 105 } 106 107 throw $e; 108 } 109 110 return ob_get_clean(); 111 } 112 113 /** 114 * Displays a template block. 115 * 116 * @param string $name The block name to render 117 * @param array $context An array of parameters to pass to the template 118 */ 119 public function displayBlock(string $name, array $context = []) 120 { 121 $this->template->displayBlock($name, $this->env->mergeGlobals($context)); 122 } 123 124 public function getSourceContext(): Source 125 { 126 return $this->template->getSourceContext(); 127 } 128 129 public function getTemplateName(): string 130 { 131 return $this->template->getTemplateName(); 132 } 133 134 /** 135 * @internal 136 * 137 * @return Template 138 */ 139 public function unwrap() 140 { 141 return $this->template; 142 } 143} 144 145class_alias('Twig\TemplateWrapper', 'Twig_TemplateWrapper'); 146