* 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
}
/**
+ * 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.
*
* @return array An array of unary operators
$compiler->write("\$context = \$this->env->getGlobals();\n\n");
} else {
$compiler
- ->write("\$context = \$this->mergeContextWithGlobals(array(\n")
+ ->write("\$context = \$this->env->mergeGlobals(array(\n")
->indent()
;
*/
public function display(array $context, array $blocks = array())
{
- $this->displayWithErrorHandling($this->mergeContextWithGlobals($context), $blocks);
+ $this->displayWithErrorHandling($this->env->mergeGlobals($context), $blocks);
}
/**
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 {
--- /dev/null
+--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
array($node, <<<EOF
public function getfoo(\$foo = null)
{
- \$context = \$this->mergeContextWithGlobals(array(
+ \$context = \$this->env->mergeGlobals(array(
"foo" => \$foo,
));