Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm attempting to learn the Template Toolkit and I'm having trouble even getting started. Can anyone see what I'm doing wrong?
#!/usr/bin/perl use strict; use Template; my $template = new Template; my $file = 'tt/template.html'; my $data = { env => \%ENV, }; print "Content-type: text/html\n\n"; $template->process($file, $data) || die $template->error;
the template file:
<html> <head> </head> <body> <ul> [% foreach thing in env.keys.sort %] <li><b>{% thing %}:</b>[% env.thing %]</li> [% end %] </ul> </body> </html>
the output:
# perl template.pl Content-type: text/html file error - parse error - tt/template.html line 12: unexpected token +(thing) [% foreach thing in env.keys.sort %]
I even tried a
[% get env %]
at the top of my template and that didn't seem to do any good either.

Thanks.
AM

Replies are listed 'Best First'.
Re: Beginer TT question
by davidrw (Prior) on Jul 05, 2005 at 21:26 UTC
    [% FOREACH thing = env.keys.sort %] <li><b>[% thing %]:</b>[% env.$thing %]</li> [% END %]
    so you were close .. few points:
    • case matters (unless you explcitly say otherwise w/the ANYCASE option -- see docs)
    • check the docs http://www.template-toolkit.org/ for the loop syntax and other control statements (IF, etc)
    • note the $ that's needed in [% env.$thing %] -- without it, you're getting the key 'thing' and not the key that matches the value of thing.
      Thanks for the tips.

      AM

Re: Beginer TT question
by jeffa (Bishop) on Jul 05, 2005 at 21:23 UTC

    So close ... it's FOREACH thing = env.keys.sort That's =, not in. Try this instead:

    [% FOREACH thing = env.keys.sort %] [% thing %] [% END %]
    UPDATE: yes ... case matters. :)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      So close ... it's FOREACH thing = env.keys.sort That's =, not in.

      Case sensitivity aside, `IN' is a synonym for `=' in this context, as mentioned in Template::Manual::Directives

      </pedant>

      MB
      that doesn't work either. I tried it before I posted.