Hello.  I am confused about how local/global variables work within a FOREACH 
loop.  Consider this Perl program:

    use Template;
    my %data = (
        'presidents' => [
            {first_name => 'Bill', last_name  => 'Clinton'},
            {first_name => 'George'},
            {first_name => 'Barack', last_name  => 'Obama'},
        ],
    );
    my $tt = Template->new();
    $tt->process('test.html', \%data);


and this "test.html" file:


    [% FOREACH p IN presidents %]
    Name: [% p.first_name %] [% p.last_name %]<BR>
    [% END %]

    [% FOREACH presidents %]
    Name: [% first_name %] [% last_name %]<BR>
    [% END %]


>From what I gathered on the Template::Manual::Directives page, the second 
>FOREACH loop is just the short form of the first version.  And yet the output 
>in the second case is unexpected:

    Name: Bill Clinton<BR>
    Name: George <BR>
    Name: Barack Obama<BR>

    Name: Bill Clinton<BR>
    Name: George Clinton<BR>
    Name: Barack Obama<BR>

In the first case, "George" has no last name, so no value is printed.  In the 
second case, it appears "George" inherits the "last_name" value from the 
previous iteration.

Is this the intended operation?  
                                          
_______________________________________________
templates mailing list
templates@template-toolkit.org
http://mail.template-toolkit.org/mailman/listinfo/templates

Reply via email to