From fc85a16886834f011ad8d63d560b36a585f1b2e5 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Wed, 28 Dec 2011 23:05:59 +0100 Subject: [PATCH] refactored Twig_Template::display() to ease its extension --- CHANGELOG | 1 + lib/Twig/Template.php | 40 +++++++++++++++++++++++++--------------- 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/CHANGELOG b/CHANGELOG index a53f805..777ab65 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,5 +1,6 @@ * 1.5.0-RC2 + * refactored Twig_Template::display() to ease its extension * added a number_format filter * 1.5.0-RC1 (2011-12-26) diff --git a/lib/Twig/Template.php b/lib/Twig/Template.php index 9386196..7d09598 100644 --- a/lib/Twig/Template.php +++ b/lib/Twig/Template.php @@ -227,21 +227,7 @@ abstract class Twig_Template implements Twig_TemplateInterface */ public function display(array $context, array $blocks = array()) { - // we don't use array_merge as the context being generally - // bigger than globals, this code is faster. - foreach ($this->env->getGlobals() as $key => $value) { - if (!array_key_exists($key, $context)) { - $context[$key] = $value; - } - } - - try { - $this->doDisplay($context, $blocks); - } catch (Twig_Error $e) { - throw $e; - } catch (Exception $e) { - throw new Twig_Error_Runtime(sprintf('An exception has been thrown during the rendering of a template ("%s").', $e->getMessage()), -1, null, $e); - } + $this->displayWithErrorHandling($this->mergeContextWithGlobals($context), $blocks); } /** @@ -264,6 +250,30 @@ abstract class Twig_Template implements Twig_TemplateInterface return ob_get_clean(); } + protected function mergeContextWithGlobals(array $context) + { + // we don't use array_merge as the context being generally + // bigger than globals, this code is faster. + foreach ($this->env->getGlobals() as $key => $value) { + if (!array_key_exists($key, $context)) { + $context[$key] = $value; + } + } + + return $context; + } + + protected function displayWithErrorHandling(array $context, array $blocks = array()) + { + try { + $this->doDisplay($context, $blocks); + } catch (Twig_Error $e) { + throw $e; + } catch (Exception $e) { + throw new Twig_Error_Runtime(sprintf('An exception has been thrown during the rendering of a template ("%s").', $e->getMessage()), -1, null, $e); + } + } + /** * Auto-generated method to display the template with the given context. * -- 1.7.2.5