Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

What you want to do is possible, but requires you to use an underused feature in HTML::Template called loop_context_vars. Enabling it in the new() constructor will allow you to use the loop context variables: __FIRST__, __LAST__, __INNER__, __ODD__ inside your TMPL_IF or TMPL_UNLESS tags. To see what I mean, check out the template example at the bottom of this post.

In particular, we care about the __ODD__ variable, which will be set to true on the odd numbered passes. By testing this variable, we can print either the <tr> or </tr> tags when appropriate. Consider the following code:

#!/usr/bin/perl -w use strict; use HTML::Template; my @scores = ( { name => 'name1', score => 4 }, { name => 'name2', score => 2 }, { name => 'name3', score => 3 }, ); my $template = HTML::Template->new( filename => 'scores.tmpl', loop_context_vars => 1, ); $template->param(scores => \@scores); print $template->output;

Which loads a template that contains the following text:

<table> <TMPL_LOOP NAME='scores'> <TMPL_IF NAME='__ODD__'> <tr> </TMPL_IF> <td><TMPL_VAR NAME='name'></td><td><TMPL_VAR NAME='score'></td> <TMPL_UNLESS NAME='__ODD__'> </tr> </TMPL_UNLESS> </TMPL_LOOP> </table>

This will iterate over the @scores and print them in two columns in the table. There is only one drawback to this approach, if the @scores has an odd number of elements, the final </tr> will not be printed. This can either be solved by pushing an empty element onto the array, or creative use of the __LAST__ and __ODD__ variables together. I personally prefer the latter and try to offload presentation logic off to the template, rather than pollute the code with special exceptions.


In reply to (dkubb) Re: (2) HTML::Template - Two columns? by dkubb
in thread HTML::Template - Two columns? by mikeB

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others scrutinizing the Monastery: (5)
    As of 2019-05-24 18:01 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Do you enjoy 3D movies?



      Results (151 votes). Check out past polls.

      Notices?
      • (Sep 10, 2018 at 22:53 UTC) Welcome new users!