The CSS solution is good and is a solution that I would advocate.
But sometimes it is hard to make CSS fit your case and sometimes you really do want to use tables (ie for non-changing grid layouts). In these cases - you can use a template to do everything - if the template language has enough mini-language to it. The following is a sample from the Template Toolkit 2 dialect (even though I show CGI::Ex::Template - it would also work with Template::Toolkit).
use CGI::Ex::Template;
my @a = qw(one two three four five six seven eight);
my $temp = q{
[%~ cols = 3 %]
[%~ rows = a.size DIV cols %]
[%~ IF a.size % cols ; rows = rows + 1 ; END -%]
<table>
[%- FOR i IN [1 .. rows] %]
<tr>
[%- FOR j IN [1 .. cols] %]
[%~ k = (i - 1) * cols + (j - 1) %]
<td>[% a.$k %]</td>
[%- END %]
</tr>
[%- END %]
</table>
};
CGI::Ex::Template->new->process(\$temp, {a => \@a}) || die $CGI::Ex::T
+emplate::error;
The number of columns is adjusted by changing cols = 3 to whatever you'd like. That code prints out:
<table>
<tr>
<td>one</td>
<td>two</td>
<td>three</td>
</tr>
<tr>
<td>four</td>
<td>five</td>
<td>six</td>
</tr>
<tr>
<td>seven</td>
<td>eight</td>
<td></td>
</tr>
</table>
my @a=qw(random brilliant braindead); print $a[rand(@a)];
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.