You can do this very easily with HTML::Template::Expr. Just activate the loop_context_vars options so that you have access to the loop counter __counter__. Then use the expression <TMPL_UNLESS __counter__ % 4> to mark your table boundaries.

use HTML::Template::Expr; my $t = HTML::Template::Expr->new(filehandle => *DATA, loop_context_vars => 1); $t->param(data => [ map { {value => $_} } ('a'..'z') ] ); print $t->output; __DATA__ <table> <tr><TMPL_LOOP NAME="DATA"><td> <TMPL_VAR NAME="VALUE"> </td><TMPL_UN +LESS EXPR="(__counter__ % 4) || (__last__)"> </tr> <tr></TMPL_UNLESS></TMPL_LOOP></tr> </table>

This will result in the following table:

<table> <tr><td> a </td><td> b </td><td> c </td><td> d </td> </tr> <tr><td> e </td><td> f </td><td> g </td><td> h </td> </tr> <tr><td> i </td><td> j </td><td> k </td><td> l </td> </tr> <tr><td> m </td><td> n </td><td> o </td><td> p </td> </tr> <tr><td> q </td><td> r </td><td> s </td><td> t </td> </tr> <tr><td> u </td><td> v </td><td> w </td><td> x </td> </tr> <tr><td> y </td><td> z </td></tr> </table>

If the designer wishes to change the number of columns, just change the 4 to the desired number...

This kind of goes against the philosophy of HTML::Template by making the tempate look more complex than desired. However, I think this is the only way to allow the template to decide how to split up the columns. The other options mentioned leave that up to the programmer which is not always what you want. I think the number of columns to display is a design decision, not a programming decision.

By the way, if you search the HTML::Template archives you will find many answers to this question...

- Cees


In reply to Re: Column Count w/ HTML::Template by cees
in thread Column Count w/ HTML::Template by dejoha

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.