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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |