Anthony Gardner <[EMAIL PROTECTED]> writes:

> I did post this before Christmas but got no responses so will try again.
>
> What's better in terms of squeezing as much out of TT as possible and 
> readability?
>
> [% FOREACH item IN list;
>        do processing
>       data_var = "result of processing"
> %]
>
>   <td>[% data_var  %]</td>
>
> [% END %]

This has always been my favourite style: Make the resulting text stick
out as visible as possible.  Troubleshooting and extending will both
be way easier if templates and output resemble each other.

> or .....
>
> [%
> FOREACH item IN list;
>     do processing;
>     data_var = "result of processing";
>     "<td>$data_var</td>";
> END
> %]
>
> or ...........
>
> [%
> FOREACH item IN list;
>     do processing;
>     data_var = "result of processing";
>     '<td>' _ data_var _ '</td>';
> END
> %]

These both make it very difficult to see what will be expanded, and in
my opinion prone to errors for longer strings (was that ' _ ' or
'text' _ 'more text'?)

> I ask these questions because there have been 2/3 of us working on
> this and now we've gone live, we're now comparing style.
>
> There might be more BP questions later. Hope you don't mind.

If you don't mind, let me list a couple of conventions which are
unrelated to your example but which I find helpful for "fat"
templates:

  * Make a naming convention to distinguish between template files,
    BLOCKs, and MACROs, so that they can easily be found.
    Paraphrasing Damian Conway, Perl Best Practices: Consider that a
    maniac who knows where you live needs to change your template in
    six months from now.  Or consider that the maniac be yourself.

  * Templates and blocks should either closely resemble the desired
    output ("print templates") or not output anything at all
    ("processing templates").  Print templates should have no "side
    effects", e.g. not change any TT variables.  There is no syntactic
    distinction in TT for both types, but an analogous advice is often
    given for XSLT templates and TeX macros.

  * Define processing templates, blocks and macros at the beginning of
    your template.  This will make programmers familiar with their
    habits of having C header files, Perl modules and the like
    declared at the beginning of their sources.

  * End this declaration section with a [% CLEAR %] statement.  This
    will help as a clearly visible distinction to the reader, and kill
    all whitespace or other stuff which your TT directives might have
    inadvertedly expanded.

  * Especially for XML/HTML output: Let templates and blocks output
    one complete and valid element each.

Of course, for simple templates this might be just overengineering....
-- 
Cheers,
haj


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

Reply via email to