$compiler
->write("\$context['_seq'] = twig_iterator_to_array(")
->subcompile($this->seq)
- ->raw(", ".(null !== $this->key_target ? 'true' : 'false').");\n")
+ ->raw(");\n")
;
if ($this['with_loop']) {
$compiler
- ->write("\$length = count(\$context['_seq']);\n")
+ ->write("\$countable = is_array(\$context['_seq']) || (is_object(\$context['_seq']) && \$context['_seq'] instanceof Countable);\n")
+ ->write("\$length = \$countable ? count(\$context['_seq']) : null;\n")
->write("\$context['loop'] = array(\n")
- ->write(" 'parent' => \$context['_parent'],\n")
- ->write(" 'length' => \$length,\n")
- ->write(" 'index0' => 0,\n")
- ->write(" 'index' => 1,\n")
- ->write(" 'revindex0' => \$length - 1,\n")
- ->write(" 'revindex' => \$length,\n")
- ->write(" 'first' => true,\n")
- ->write(" 'last' => 1 === \$length,\n")
+ ->write(" 'parent' => \$context['_parent'],\n")
+ ->write(" 'index0' => 0,\n")
+ ->write(" 'index' => 1,\n")
+ ->write(" 'first' => true,\n")
->write(");\n")
+ ->write("if (\$countable) {\n")
+ ->indent()
+ ->write("\$context['loop']['revindex0'] = \$length - 1;\n")
+ ->write("\$context['loop']['revindex'] = \$length;\n")
+ ->write("\$context['loop']['length'] = \$length;\n")
+ ->write("\$context['loop']['last'] = 1 === \$length;\n")
+ ->outdent()
+ ->write("}\n")
;
}
$compiler
->write("++\$context['loop']['index0'];\n")
->write("++\$context['loop']['index'];\n")
+ ->write("\$context['loop']['first'] = false;\n")
+ ->write("if (\$countable) {\n")
+ ->indent()
->write("--\$context['loop']['revindex0'];\n")
->write("--\$context['loop']['revindex'];\n")
- ->write("\$context['loop']['first'] = false;\n")
->write("\$context['loop']['last'] = 0 === \$context['loop']['revindex0'];\n")
+ ->outdent()
+ ->write("}\n")
;
}
$tests[] = array($node, <<<EOF
\$context['_parent'] = (array) \$context;
-\$context['_seq'] = twig_iterator_to_array(\$this->getContext(\$context, 'items'), true);
+\$context['_seq'] = twig_iterator_to_array(\$this->getContext(\$context, 'items'));
foreach (\$context['_seq'] as \$context['key'] => \$context['item']) {
echo \$this->getContext(\$context, 'foo');
}
$tests[] = array($node, <<<EOF
\$context['_parent'] = (array) \$context;
-\$context['_seq'] = twig_iterator_to_array(\$this->getContext(\$context, 'values'), true);
-\$length = count(\$context['_seq']);
+\$context['_seq'] = twig_iterator_to_array(\$this->getContext(\$context, 'values'));
+\$countable = is_array(\$context['_seq']) || (is_object(\$context['_seq']) && \$context['_seq'] instanceof Countable);
+\$length = \$countable ? count(\$context['_seq']) : null;
\$context['loop'] = array(
- 'parent' => \$context['_parent'],
- 'length' => \$length,
- 'index0' => 0,
- 'index' => 1,
- 'revindex0' => \$length - 1,
- 'revindex' => \$length,
- 'first' => true,
- 'last' => 1 === \$length,
+ 'parent' => \$context['_parent'],
+ 'index0' => 0,
+ 'index' => 1,
+ 'first' => true,
);
+if (\$countable) {
+ \$context['loop']['revindex0'] = \$length - 1;
+ \$context['loop']['revindex'] = \$length;
+ \$context['loop']['length'] = \$length;
+ \$context['loop']['last'] = 1 === \$length;
+}
foreach (\$context['_seq'] as \$context['k'] => \$context['v']) {
echo \$this->getContext(\$context, 'foo');
++\$context['loop']['index0'];
++\$context['loop']['index'];
- --\$context['loop']['revindex0'];
- --\$context['loop']['revindex'];
\$context['loop']['first'] = false;
- \$context['loop']['last'] = 0 === \$context['loop']['revindex0'];
+ if (\$countable) {
+ --\$context['loop']['revindex0'];
+ --\$context['loop']['revindex'];
+ \$context['loop']['last'] = 0 === \$context['loop']['revindex0'];
+ }
}
\$_parent = \$context['_parent'];
unset(\$context['_seq'], \$context['_iterated'], \$context['k'], \$context['v'], \$context['_parent'], \$context['loop']);
$tests[] = array($node, <<<EOF
\$context['_parent'] = (array) \$context;
\$context['_iterated'] = false;
-\$context['_seq'] = twig_iterator_to_array(\$this->getContext(\$context, 'values'), true);
-\$length = count(\$context['_seq']);
+\$context['_seq'] = twig_iterator_to_array(\$this->getContext(\$context, 'values'));
+\$countable = is_array(\$context['_seq']) || (is_object(\$context['_seq']) && \$context['_seq'] instanceof Countable);
+\$length = \$countable ? count(\$context['_seq']) : null;
\$context['loop'] = array(
- 'parent' => \$context['_parent'],
- 'length' => \$length,
- 'index0' => 0,
- 'index' => 1,
- 'revindex0' => \$length - 1,
- 'revindex' => \$length,
- 'first' => true,
- 'last' => 1 === \$length,
+ 'parent' => \$context['_parent'],
+ 'index0' => 0,
+ 'index' => 1,
+ 'first' => true,
);
+if (\$countable) {
+ \$context['loop']['revindex0'] = \$length - 1;
+ \$context['loop']['revindex'] = \$length;
+ \$context['loop']['length'] = \$length;
+ \$context['loop']['last'] = 1 === \$length;
+}
foreach (\$context['_seq'] as \$context['k'] => \$context['v']) {
\$context['_iterated'] = true;
echo \$this->getContext(\$context, 'foo');
++\$context['loop']['index0'];
++\$context['loop']['index'];
- --\$context['loop']['revindex0'];
- --\$context['loop']['revindex'];
\$context['loop']['first'] = false;
- \$context['loop']['last'] = 0 === \$context['loop']['revindex0'];
+ if (\$countable) {
+ --\$context['loop']['revindex0'];
+ --\$context['loop']['revindex'];
+ \$context['loop']['last'] = 0 === \$context['loop']['revindex0'];
+ }
}
if (!\$context['_iterated']) {
echo \$this->getContext(\$context, 'foo');