Great. I adore TT and this example goes to the root of why and why I break from the View purists who say TT is a disaster and you should use something like HTML::Template. These simple loop controls can solve many common display logic problems.

TT can do just about anything. Beware of moving too much into it (it can write files, run macros, run plain Perl, do recursion, etc, etc, etc). If you want a ton of code in templates, Mason is probably the right choice. TT lets you separate and plugin really well though so you can do your own methods and filters, for example, without writing anything but glue code; The Right Way to Do It™. Just another example-

use warnings; use strict; use Template; use Lingua::EN::Numbers::Ordinate "ordinate"; my %config = ( FILTERS => { ordinal => sub { ordinate($_[0]) }, ordinal_html => sub { my $ord = ordinate($_[0]); $ord =~ s,(\D\D)\z,<sup>$1</sup>,; return $ord; } } ); my $tt2 = Template->new(\%config); $tt2->process(\*DATA, { numbers => [ 0 .. 121 ] }) or die $tt2->error; __DATA__ [% FOR i IN numbers %] Plain: [% i | ordinal %] -- HTML: [% i | ordinal_html %] [%-END %]

I'd also point you toward Template::Alloy. It's a well behaved implementation of the TT2(3) syntax with a few improvements and supports several other template types. I've used it on my last couple of projects and haven't hit any reasons to go back.


In reply to Re^3: Format and Join an Array in a Template by Your Mother
in thread Format and Join an Array in a Template by mscharrer

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.