[Omaha.pm] Template Toolkit syntax choices, whitespace

Jay Hannah jhannah at omnihotels.com
Fri Jul 15 13:00:27 PDT 2005


Ever played with Template Toolkit? If you're accustomed to Perl 5 syntax, TT has multiple syntaxes to choose from, all different from Perl 5 syntax. 

For instance, I just changed this TT code:

[% FOREACH selection = options %]
  [% IF display_type == 'code' %]
    [% IF selection.code == default %]
      <OPTION selected value="${selection.code}">${selection.code}</OPTION>
    [% ELSE %]
      <OPTION value="${selection.code}">${selection.code}</OPTION>
    [% END %]
  [% ELSE %]
    [% IF selection.code == default OR q.param(selectname) == selection.code %]
      <OPTION selected value="${selection.code}">${selection.desc}</OPTION>
    [% ELSE %]
      <OPTION value="${selection.code}">${selection.desc}</OPTION>
    [% END %]
  [% END %]
[% END %]

To this TT code:

[%-
  selected = "";
  value = selection.desc;
  FOREACH selection = options;
    IF display_type == 'code';
      value = selection.code;
      IF selection.code == default;
        selected = "selected";
      END;
    ELSE;
      IF selection.code == default OR q.param(selectname) == selection.code;
        selected = "selected";
      END;
    END;
    "   <OPTION $selected value='${selection.code}'>${selection.desc}</OPTION>\n";
    selected = "";
  END;
-%]

Those two snippets do almost the same thing. The primary difference is the first outputs tons of unintentional whitespace. In the second you don't have to use [% %] everywhere... (I tried fighting the whitespace with [%- -%] everywhere to no avail.)

After 2 weeks of full time TT, I think I'm starting to get pretty good at it. It's does some really amazing things if you're building large, extremely modular websites. The book says its good for other stuff too.

Looks like our total TT footprint is 15K lines of control code and 8K lines of TT templates... woof!

j




More information about the Omaha-pm mailing list