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