* 0.9.6-DEV
+ * fixed variables defined outside a loop and for which the value changes in a for loop
* fixed the test suite for PHP 5.2 and older versions of PHPUnit
* added support for __call() in expression resolution
* fixed node visiting for macros (macros are now visited by visitors as any other node)
}
/**
- * Pushes the current context on the stack.
- *
- * @return Twig_Compiler The current compiler instance
- */
- public function pushContext()
- {
- // the (array) cast bypasses a PHP 5.2.6 bug
- $this->write('$context[\'_parent\'] = (array) $context;'."\n");
-
- return $this;
- }
-
- /**
- * Pops a context from the stack.
- *
- * @return Twig_Compiler The current compiler instance
- */
- public function popContext()
- {
- $this->write('$context = $context[\'_parent\'];'."\n");
-
- return $this;
- }
-
- /**
* Adds debugging information.
*
* @param Twig_Node $node The related twig node
{
$compiler
->addDebugInfo($this)
- ->pushContext()
+ // the (array) cast bypasses a PHP 5.2.6 bug
+ ->write('$context[\'_parent\'] = (array) $context;'."\n")
;
if (!is_null($this->else)) {
->write("}\n")
;
}
- $compiler->popContext();
+
+ $compiler->write('$_parent = $context[\'_parent\'];'."\n");
+
+ // remove some "private" loop variables (needed for nested loops)
+ $compiler->write('unset($context[\'_seq\'], $context[\'_iterated\'], $context[\''.$loopVars[0].'\'], $context[\''.$loopVars[1].'\'], $context[\'_parent\'], $context[\'loop\']);'."\n");
+
+ /// keep the values set in the inner context for variables defined in the outer context
+ $compiler->write('$context = array_merge($_parent, array_intersect_key($context, $_parent));'."\n");
}
public function setWithLoop($boolean)
--- /dev/null
+--TEST--
+"for" tag does not reset inner variables
+--TEMPLATE--
+{% for i in 1..2 %}
+ {% for j in 0..2 %}
+ {{k}}{% set k as k+1 %} {{ loop.parent.loop.index }}
+ {% endfor %}
+{% endfor %}
+--DATA--
+return array('k' => 0)
+--EXPECT--
+ 0 1
+ 1 1
+ 2 1
+ 3 2
+ 4 2
+ 5 2