4 * This file is part of Twig.
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
13 * Exposes a template to userland.
15 * @author Fabien Potencier <fabien@symfony.com>
17 final class Twig_TemplateWrapper
23 * This method is for internal use only and should never be called
24 * directly (use Twig_Environment::load() instead).
28 public function __construct(Twig_Environment $env, Twig_Template $template)
31 $this->template = $template;
35 * Renders the template.
37 * @param array $context An array of parameters to pass to the template
39 * @return string The rendered template
41 public function render($context = array())
43 return $this->template->render($context);
47 * Displays the template.
49 * @param array $context An array of parameters to pass to the template
51 public function display($context = array())
53 $this->template->display($context);
57 * Checks if a block is defined.
59 * @param string $name The block name
60 * @param array $context An array of parameters to pass to the template
64 public function hasBlock($name, $context = array())
66 return $this->template->hasBlock($name, $context);
70 * Returns defined block names in the template.
72 * @param array $context An array of parameters to pass to the template
74 * @return string[] An array of defined template block names
76 public function getBlockNames($context = array())
78 return $this->template->getBlockNames($context);
82 * Renders a template block.
84 * @param string $name The block name to render
85 * @param array $context An array of parameters to pass to the template
87 * @return string The rendered block
89 public function renderBlock($name, $context = array())
91 $context = $this->env->mergeGlobals($context);
92 $level = ob_get_level();
95 $this->template->displayBlock($name, $context);
96 } catch (Exception $e) {
97 while (ob_get_level() > $level) {
102 } catch (Throwable $e) {
103 while (ob_get_level() > $level) {
110 return ob_get_clean();
114 * Displays a template block.
116 * @param string $name The block name to render
117 * @param array $context An array of parameters to pass to the template
119 public function displayBlock($name, $context = array())
121 $this->template->displayBlock($name, $this->env->mergeGlobals($context));
125 * @return Twig_Source
127 public function getSourceContext()
129 return $this->template->getSourceContext();