Anthony Gardner wrote:
> What's better in terms of squeezing as much out of TT as possible and 
> readability?

Hi Anthony,

When things start getting complicated, I usually reach for a separate
BLOCK.  I find it helps to keep the high level logic simple and easy
to read:

   [% FOREACH item IN list;             # no HTML, just logic in this bit
        do processing;
        data_var = "result of processing";
        PROCESS item_output;
      END;
   %]

   [% BLOCK item_output -%]     # just HTML, no display logic
      <td>[% data_var  %]</td>
   [% END %]

Or you might prefer to create a separate template file that you can
call from elsewhere:

table/td:
    <td>[% content %]</td>

'content' is a good choice for a variable name in these cases so that
it'll work automagically with the WRAPPER directive:

    [% FOREACH item IN list;
         WRAPPER table/td;
           do_processing;
           "result of processing";
         END;
       END;
    %]

You could also create a MACRO:

Either:
    [% MACRO td(content) GET "<td>$content</td>" %]   # inline expansion

Or:
    [% MACRO td(content) PROCESS table/td %]          # via a template

And then:

    [% FOREACH item IN list;
         do_processing;
         td("result of processing");
       END;
    %]

The MACRO approach can also be useful when you've got more complex data to
output.  e.g.

    [% MACRO key_value(key, value) BLOCK -%]
       <td class="key">[% key %]</td>
       <td class="value">[% value %]</td>
    [% END %]

    [% FOREACH product IN list;
         key_value(product.code, product.price);
       END;
    %]

Sorry that I don't have a single definitive answer for you, but with matters
of style there rarely is a "right" answer.  Hopefully the above examples gives
you some ideas to get you heading the in the right direction.  I suspect the
real answer is that it matters less which style you choose and more that your
team agree on one style and use it consistently wherever possible.

Cheers
A



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

Reply via email to