The first variant is different from the others because it also inserts
some whitespace.
I personally prefer the second variant. But I also think you can't
define "best practice" for cases like this—it depends on the actual
conditions. In your sample you only output a small <td>, which is why
I'd chose second variant. But if you need to output lots of HTML, then
the first one might be more readable.
On the other hand: your sample is small and works well in a template,
but if your code gets really complex then you should move it in some
Perl function and call it from your template. I.e. you can define a
function that processes list and returns an array with processed values:
sub process_list {
my ($list) = @_;
my @result = map { do_processing($_) } @$list;
return [EMAIL PROTECTED];
}
and in your template you'd write:
[% BLOCK display_data_var %]<td>[% data_var %]</td>[% END %]
[% PROCESS display_data_var FOREACH data_var = process_list(list) %]
Or something like this. ;-) Move complexity out of your templates for
good readability.
-Mihai
Anthony Gardner wrote:
> 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 %]
>
> 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
> %]
>
> 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.
>
> CIA
>
> -Ants
>
>
> Disclaimer: Technically, I'm always wrong!!
>
> ------------------------------------------------------------------------
> Sent from Yahoo!
> <http://us.rd.yahoo.com/mailuk/taglines/isp/control/*http://us.rd.yahoo.com/evt=51949/*http://uk.docs.yahoo.com/mail/winter07.html>
> - a smarter inbox.
> ------------------------------------------------------------------------
>
> _______________________________________________
> templates mailing list
> [email protected]
> http://mail.template-toolkit.org/mailman/listinfo/templates
>
_______________________________________________
templates mailing list
[email protected]
http://mail.template-toolkit.org/mailman/listinfo/templates