Skip to content

Commit

Permalink
refactored Twig_Template::display() to ease its extension
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Dec 28, 2011
1 parent 88de457 commit fc85a16
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
1 change: 1 addition & 0 deletions 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)
Expand Down
40 changes: 25 additions & 15 deletions lib/Twig/Template.php
Expand Up @@ -227,21 +227,7 @@ public function getBlocks()
*/
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);
}

/**
Expand All @@ -264,6 +250,30 @@ public function render(array $context)
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.
*
Expand Down

0 comments on commit fc85a16

Please sign in to comment.