Thanks for the softball.
Here is your job. I will give you an array of text strings. Without manipulating the data in the perl layer, please provide one template that shows them in a bullet list and another template that shows them in a table with three columns with data oriented in columns with one item per cell. On the column oriented version fill in
wherever there isn't a defined value. Be sure external whitespace is nice and consistent.
For extra credit - make the template decide conditionally that if you have less than n items use the bullet list - otherwise use the table.
For extra extra credit - do this exercise - but do it in a text-only based email that will be sent to a user (ie - no html tags).
----- bullet.tt ---------
<ul>
[%- FOREACH i IN items %]
<li>[% i %]</li>
[%- END %]
</ul>
----- columns.tt --------
[%- cols = 3;
rows = items.size div cols;
rows = rows+1 IF items.size % cols %]
[%- FOR i IN [0 .. rows - 1] %]
<tr>
[%- FOR j IN [0 .. cols -1] %]
<td>[% items.${ i + rows * j } %]</td>
[%- END %]
</tr>
[%- END %]
</table>
------ optional.tt
[% PROCESS ${ items.size > 10 ? "columns.tt" : "bullet.tt" } %]
------ my_perl.pl ------
use Template::Alloy;
my $t = Template::Alloy->new;
my $data = {
items => [1 .. 10],
};
$t->process("bullet.tt", $data) || die $t->error;
$t->process("columns.tt", $data) || die $t->error;
__END__
prints
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
<li>6</li>
<li>7</li>
<li>8</li>
<li>9</li>
<li>10</li>
</ul>
<table>
<tr>
<td>1</td>
<td>5</td>
<td>9</td>
</tr>
<tr>
<td>2</td>
<td>6</td>
<td>10</td>
</tr>
<tr>
<td>3</td>
<td>7</td>
<td> </td>
</tr>
<tr>
<td>4</td>
<td>8</td>
<td> </td>
</tr>
</table>
Update To me the code layer would be your perl modules and your cgi script (or your mod_perl application). The example given is simplified and contrived - but represents real life situations.
Update 2 s/specifying/manipulating the data/
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.