Skip to content

Commit

Permalink
added Twig_Environment::mergeGlobals()
Browse files Browse the repository at this point in the history
  • Loading branch information
fabpot committed Apr 3, 2012
1 parent 3398b38 commit f2f1f32
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
@@ -1,5 +1,6 @@
* 1.7.0 (2012-XX-XX)

* added Twig_Environment::mergeGlobals()
* fixed a regression when a template only extends another one without defining any blocks
* added compilation checks to avoid misuses of the sandbox tag
* fixed filesystem loader freshness logic for high traffic websites
Expand Down
20 changes: 20 additions & 0 deletions lib/Twig/Environment.php
Expand Up @@ -972,6 +972,26 @@ public function getGlobals()
return $this->globals;
}

/**
* Merges a context with the defined globals.
*
* @param array $context An array representing the context
*
* @return array The context merged with the globals
*/
public function mergeGlobals(array $context)
{
// we don't use array_merge as the context being generally
// bigger than globals, this code is faster.
foreach ($this->getGlobals() as $key => $value) {
if (!array_key_exists($key, $context)) {
$context[$key] = $value;
}
}

return $context;
}

/**
* Gets the registered unary Operators.
*
Expand Down
2 changes: 1 addition & 1 deletion lib/Twig/Node/Macro.php
Expand Up @@ -44,7 +44,7 @@ public function compile(Twig_Compiler $compiler)
$compiler->write("\$context = \$this->env->getGlobals();\n\n");
} else {
$compiler
->write("\$context = \$this->mergeContextWithGlobals(array(\n")
->write("\$context = \$this->env->mergeGlobals(array(\n")
->indent()
;

Expand Down
15 changes: 1 addition & 14 deletions lib/Twig/Template.php
Expand Up @@ -236,7 +236,7 @@ public function getBlocks()
*/
public function display(array $context, array $blocks = array())
{
$this->displayWithErrorHandling($this->mergeContextWithGlobals($context), $blocks);
$this->displayWithErrorHandling($this->env->mergeGlobals($context), $blocks);
}

/**
Expand All @@ -259,19 +259,6 @@ 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 {
Expand Down
14 changes: 14 additions & 0 deletions test/Twig/Tests/Fixtures/tags/macro/global.test
@@ -0,0 +1,14 @@
--TEST--
"macro" tag
--TEMPLATE--
{% from 'forms.twig' import foo %}

{{ foo('foo') }}
{{ foo() }}
--TEMPLATE(forms.twig)--
{% macro foo(name) %}{{ name|default('foo') }}{{ global }}{% endmacro %}
--DATA--
return array()
--EXPECT--
fooglobal
fooglobal
2 changes: 1 addition & 1 deletion test/Twig/Tests/Node/MacroTest.php
Expand Up @@ -46,7 +46,7 @@ public function getTests()
array($node, <<<EOF
public function getfoo(\$foo = null)
{
\$context = \$this->mergeContextWithGlobals(array(
\$context = \$this->env->mergeGlobals(array(
"foo" => \$foo,
));
Expand Down

0 comments on commit f2f1f32

Please sign in to comment.