aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/includes
diff options
context:
space:
mode:
authorNathaniel Guse <nathaniel.guse@gmail.com>2013-06-15 11:20:10 -0500
committerNathaniel Guse <nathaniel.guse@gmail.com>2013-06-15 11:20:10 -0500
commitc5db8be580679427a906a9c44d696cd8d9bfc76e (patch)
treea45ead125b77f3d8a69125f3ead5a673d5b604bc /phpBB/includes
parent8561e187f0e5949750a61bffbd16ab4f56ae7fc1 (diff)
downloadforums-c5db8be580679427a906a9c44d696cd8d9bfc76e.tar
forums-c5db8be580679427a906a9c44d696cd8d9bfc76e.tar.gz
forums-c5db8be580679427a906a9c44d696cd8d9bfc76e.tar.bz2
forums-c5db8be580679427a906a9c44d696cd8d9bfc76e.tar.xz
forums-c5db8be580679427a906a9c44d696cd8d9bfc76e.zip
[feature/twig] Fix begin loops & subloops
PHPBB3-11598
Diffstat (limited to 'phpBB/includes')
-rw-r--r--phpBB/includes/template/twig/node/begin.php89
-rw-r--r--phpBB/includes/template/twig/twig.php6
2 files changed, 52 insertions, 43 deletions
diff --git a/phpBB/includes/template/twig/node/begin.php b/phpBB/includes/template/twig/node/begin.php
index 1f4de9deda..3adb09f17e 100644
--- a/phpBB/includes/template/twig/node/begin.php
+++ b/phpBB/includes/template/twig/node/begin.php
@@ -30,48 +30,59 @@ class phpbb_template_twig_node_begin extends Twig_Node
*/
public function compile(Twig_Compiler $compiler)
{
- $compiler
- ->write("if (!isset(\$loops)) {\n")
- ->indent()
- ->write("\$loops = array();")
- ->write("\$nestingLevel = 0;")
- ->outdent()
- ->write("}\n")
- ->write("\$loops[\$nestingLevel] = array();\n")
- ;
+ $compiler->addDebugInfo($this);
- if (null !== $this->getNode('else')) {
- $compiler->write("\$loops[\$nestingLevel]['iterated'] = false;\n");
- }
-
- $compiler
- ->write("if (isset(\$context['loop']['" . $this->getAttribute('beginName') . "'])) {")
- ->write("foreach (\$context['loop']['". $this->getAttribute('beginName'). "'] as \$" . $this->getAttribute('beginName') . ") {")
- ->write("\$context['". $this->getAttribute('beginName'). "'] = \$" . $this->getAttribute('beginName') . ";")
+ $compiler
+ // name -> loop name
+ // local context -> parent template variable context
+ // global context -> global template variable context
+ // variable chain -> full chain of variables to output template vars properly in subloops
+ // e.g. [foo][bar][foobar]
+ // current chain location -> current location in subloop
+ // e.g. [foobar] of [foo][bar]
+ ->write("\$iterator = function (\$name, \$local_context, \$global_context, &\$variable_chain, &\$current_chain_location) {\n")
->indent()
- ;
-
- $compiler->subcompile($this->getNode('body'));
+ //->write("var_dump(\$name, \$local_context);\n")
+ // Try to find the loop in the
+ // local context (child of local context passed, in case of a child loop)
+ // global context (root template var)
+ ->write("if (isset(\$local_context[\$name])) {\n")
+ ->indent()
+ ->write("\$local_context = \$local_context[\$name];\n")
+ ->outdent()
+ ->write("}\n")
+ ->write("else if (isset(\$global_context[\$name])) {\n")
+ ->indent()
+ ->write("\$local_context = \$global_context[\$name];\n")
+ ->outdent()
+ ->write("} else { return; }\n")
- if (null !== $this->getNode('else')) {
- $compiler->write("\$loops[\$nestingLevel]['iterated'] = true;\n");
- }
+ ->write("if (!is_array(\$local_context) || empty(\$local_context)) { return; }\n")
- $compiler
- ->outdent()
- ->write("}}\n")
- ;
+ ->write("foreach (\$local_context as \$for_context) {\n")
+ ->indent()
+ // Some hackish stuff for Twig to properly subcompile
+ ->write("\$current_chain_location[\$name] = \$for_context;\n")
+ ->write("\$context = array_merge(\$global_context, \$variable_chain);\n")
- if (null !== $this->getNode('else')) {
- $compiler
- ->write("if (!\$loops[\$nestingLevel]['iterated']) {\n")
- ->indent()
- ->subcompile($this->getNode('else'))
- ->outdent()
- ->write("}\n")
- ;
- }
-
- $compiler->write("\$nestingLevel--;\n");
+ // Children
+ ->subcompile($this->getNode('body'))
+ ->outdent()
+ ->write("}\n")
+ ->outdent()
+ ->write("};\n")
+ ->write("if (isset(\$global_context)) {\n")
+ ->indent()
+ // We are already inside an anonymous function
+ ->write("\$iterator('" . $this->getAttribute('beginName') . "', \$for_context, \$global_context, \$variable_chain, \$current_chain_location[\$name]);\n")
+ ->outdent()
+ ->write("} else {\n")
+ ->indent()
+ // We are not inside the anonymous function (first level)
+ ->write("\$variable_chain = array();\n")
+ ->write("\$current_chain_location = array();\n")
+ ->write("\$iterator('" . $this->getAttribute('beginName') . "', array(), \$context, \$variable_chain, \$variable_chain);\n")
+ ->outdent()
+ ->write("}\n");
}
-} \ No newline at end of file
+}
diff --git a/phpBB/includes/template/twig/twig.php b/phpBB/includes/template/twig/twig.php
index ac383e25b5..3db873c9ba 100644
--- a/phpBB/includes/template/twig/twig.php
+++ b/phpBB/includes/template/twig/twig.php
@@ -414,13 +414,11 @@ class phpbb_template_twig implements phpbb_template
$vars = array_merge(
$vars,
$this->context->get_rootref(),
- array(
- 'loop' => $this->context->get_tpldata(),
- )
+ $this->context->get_tpldata()
);
// cleanup
- unset($vars['loop']['.']);
+ unset($vars['.']);
return $vars;
}