Re: [Templates] SELECT CASE question

[prev] [thread] [next] [Date index for 2004/11/30]

From: Cees Hek
Subject: Re: [Templates] SELECT CASE question
Date: 14:43 on 30 Nov 2004
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

[Templates] SELECT CASE question
Robert 13:54 on 30 Nov 2004

Re: [Templates] SELECT CASE question
merlyn (Randal L. Schwartz) 14:34 on 30 Nov 2004

[Templates] Re: SELECT CASE question
Robert 15:00 on 30 Nov 2004

Re: [Templates] SELECT CASE question
Kenneth Ekdahl 16:23 on 30 Nov 2004

[Templates] Re: SELECT CASE question
Robert 16:47 on 30 Nov 2004

Re: [Templates] SELECT CASE question
Cees Hek 14:43 on 30 Nov 2004

Re: [Templates] SELECT CASE question
Dave Cross 14:47 on 30 Nov 2004

Generated at 08:55 on 15 Mar 2005 by mariachi v0.52