Re: "first" as a variable

Richard Thomas <[email protected]>
Newsgroups gmane.comp.lang.perl.modules.template-toolkit
Message-ID <[email protected]>
It's not a bug as such, more of a typical "reserved word" issue.

The simple workaround is to use

	category2article.item(n) 

...which tells TT to treat 'n' as an attribute, and not as a VMethod.

cheers
RET
___________________________________________________________________________
"The ability to procrastinate is what separates us from the machines." -- Chris Gregory

Richard Thomas
[email protected]


On 16/12/2011, at 12:23 PM, Ben Bullock wrote:

> In my template,
> 
> [% FOR name IN name_list %]
> [%- n = name.0 | lower %]
> [%- IF category2article.$n %]
> [% cats = category2article.$n -%]
> /* [% n %] */
> article_t cat_[% loop.count - 1 %][] = {
> [%- cats.join (", ") -%], -1};
> [%- END %]
> [%- END %]
> 
> In the usual case, this produces
> 
> /* event */
> article_t cat_748[] = {419, 1346, -1};
> 
> But when "name_list" contains the word "first",
> 
> /* first */
> article_t cat_775[] = {HASH(0x2862d2d0), -1};
> 
> This seems to be some kind of mismatch between the template toolkit's
> "first" keyword and the action of the variable $n. I am not sure if
> this is the expected behaviour, but if it is, how should I have
> phrased category2article.$n to avoid this?
> 
> At the moment I have just written a trap for it:
> 
> [% FOR name IN name_list %]
> [%- n = name.0 | lower %]
> [%- IF n != 'first' -%]
> [%- IF category2article.$n %]
> [% cats = category2article.$n -%]
> /* [% n %] */
> article_t cat_[% loop.count - 1 %][] = {
> [%- cats.join (", ") -%], -1};
> [%- END %]
> [%- END # first trap -%]
> [%- END %]
> 
> This works because I didn't need "first" as a keyword, but it's a bit
> worrying that I have to do this and I suspect a bug.
> 
> _______________________________________________
> templates mailing list
> [email protected]
> http://mail.template-toolkit.org/mailman/listinfo/templates
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.