Re: Confused about FOREACH and local variables

Chad Wallace <[email protected]> Thu, 3 Jul 2014 13:25:53 -0700
Newsgroups gmane.comp.lang.perl.modules.template-toolkit
Organization The Lodging Company
Message-ID <[email protected]>
On Thu, 3 Jul 2014 13:46:15 -0400
George <[email protected]> wrote:

>     [% FOREACH p IN presidents %]
>     Name: [% p.first_name %] [% p.last_name %]<BR>
>     [% END %]
> 
>     [% FOREACH presidents %]
>     Name: [% first_name %] [% last_name %]<BR>
>     [% END %]
[...]
> 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?  

Not sure if it's intended, but the reason it happens is because there
isn't actually any scoping in FOREACH loops.  The second form just sets
variables in the stash based on the properties of your items.  If the
property isn't there, the variable doesn't get set (or unset).

I guess it could be fixed by having the FOREACH keep track of every
variable it set for the iteration, and then unset each of them before
continuing to the next iteration.


-- 

C. Chad Wallace, B.Sc.
The Lodging Company
http://www.lodgingcompany.com/
OpenPGP Public Key ID: 0x262208A0


_______________________________________________
templates mailing list
[email protected]
http://mail.template-toolkit.org/mailman/listinfo/templates