Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nested {foreach} mucks up @index, @iteration, @first, @last of parent {foreach} #45

Closed
rsdifu opened this issue May 29, 2015 · 1 comment

Comments

@rsdifu
Copy link

rsdifu commented May 29, 2015

smarty-foreach-test.php

<?php

header("Content-Type: text/plain");

$smarty = new Smarty();
// Do your smarty setup

$array = array(
    array(
        "var" => "Parent 1 var",
        "sub" => array(
            array("var" => "Sub 1 var"),
            array("var" => "Sub 2 var")
        )
    ),
    array(
        "var" => "Parent 2 var",
        "sub" => array(
            array("var" => "Sub 1 var"),
            array("var" => "Sub 2 var")
        )
    ),
);
$smarty->assign("array", $array);
$smarty->display("smarty-foreach-test.tpl");

smarty-foreach-test.tpl

Test 1: No Nested {literal}{foreach}{/literal}
---------------------------
{foreach $array as $item}
    {$item@iteration}: {$item.var}    <--- Note @iteration (sequential, as expected)
{if $item@last}
        Last! (parent)
{/if}
{/foreach}

Test 2: With Nested {literal}{foreach}{/literal}
-----------------------------
{foreach $array as $item}
    {$item@iteration}: {$item.var}    <--- Note @iteration (not sequential)
{foreach $item.sub as $sub}
        {$sub@iteration}: {$sub.var}
{if $sub@last}
            Last! (sub)
{/if}
{/foreach}
{if $item@last}
        Last! (parent)
{/if}
{/foreach}

        ^--- Note the lack of the "Last! (parent)"

Output

Test 1: No Nested {foreach}
---------------------------
    1: Parent 1 var    <--- Note @iteration (sequential, as expected)
    2: Parent 2 var    <--- Note @iteration (sequential, as expected)
        Last! (parent)

Test 2: With Nested {foreach}
-----------------------------
    1: Parent 1 var    <--- Note @iteration (not sequential)
        1: Sub 1 var
        2: Sub 2 var
            Last! (sub)
    3: Parent 2 var    <--- Note @iteration (not sequential)
        1: Sub 1 var
        2: Sub 2 var
            Last! (sub)

        ^--- Note the lack of the "Last! (parent)"
@rsdifu
Copy link
Author

rsdifu commented May 29, 2015

Resolved in 3.1.24. Must have missed it in the commits. Remember kids, don't stay up late and debug.

@rsdifu rsdifu closed this as completed May 29, 2015
think-mcunanan pushed a commit to think-mcunanan/smarty that referenced this issue Mar 22, 2023
Merge release-2.0.5.1 release branch into release-2.0.6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant