Re: [Templates] SELECT CASE question
[prev]
[thread]
[next]
[Date index for 2004/11/30]
On Tue, 30 Nov 2004 08:54:59 -0500, Robert <sigzero@xxxxx.xxx> wrote:
> Currently I have an IF/ELSE block that determines if the loop.count is
> divisible by 2 and it sets the background color.
>
> [% IF (loop.count % 2) %]
> <tr id="trcol1">
> <td>[% report.DSTRCT_CODE %]</td>
> <td>[% report.WORK_ORDER %]</td>
> <td>[% report.CREATION_DATE %]</td>
> <td>[% report.LAST_MOD_DATE %]</td>
> <td>[% report.ORIGINATOR_ID %]</td>
> <td>[% report.SURNAME %]</td>
> <td>[% report.FIRST_NAME %]</td>
> </tr>
> [% ELSE %]
> <tr id="trcol2">
> <td>[% report.DSTRCT_CODE %]</td>
> <td>[% report.WORK_ORDER %]</td>
> <td>[% report.CREATION_DATE %]</td>
> <td>[% report.LAST_MOD_DATE %]</td>
> <td>[% report.ORIGINATOR_ID %]</td>
> <td>[% report.SURNAME %]</td>
> <td>[% report.FIRST_NAME %]</td>
> </tr>
> [% END %]
>
How about this:
[% IF reports %]
[% FOREACH report IN reports %]
<tr id="trcol[% (loop.count % 2) ? 1 : 2 %]">
<td>[% report.DSTRCT_CODE %]</td>
<td>[% report.WORK_ORDER %]</td>
<td>[% report.CREATION_DATE %]</td>
<td>[% report.LAST_MOD_DATE %]</td>
<td>[% report.ORIGINATOR_ID %]</td>
<td>[% report.SURNAME %]</td>
<td>[% report.FIRST_NAME %]</td>
</tr>
[% END %]
[% ELSE %]
<tr colspan="7">No reports</tr>
[% END %]
That removes the duplication in your original version, and handles the
case when no reports are available. If you don't like the trinary
operator (? :) you could replace it with a simple IF ELSE statement.
Also, if you don't mind renaming your styles, you can simplify it to
just use id="trcol[% loop.count % 2 %]", where your styles would then
be trcol0 and trcol1.
Cheers,
Cees
_______________________________________________
templates mailing list
templates@xxxxxxxxxxxxxxxx.xxx
http://lists.template-toolkit.org/mailman/listinfo/templates
 |
 |
Re: [Templates] SELECT CASE question
Cees Hek 14:43 on 30 Nov 2004
|