[stringtemplate-interest] alternating row colows
John Snyders
jjsnyders at rcn.com
Thu Mar 1 18:32:01 PST 2007
I'm not sure I agree with the desire to keep everything in one file and not
use group files.
In my own HTML generation with ST I quickly found that group files were the
best option. Fine grained templates provide consistency. For example to make
sure that label elements are always used with text input elements you would
create a template that includes both label and input and wires them together
with the for attribute on label. Then call that template everywhere you need
to prompt for a text string. The named templates in a group file serve the
same purpose as tag libraries in JSP.
Anyway, here is a thought of how to do it with parallel list iteration.
The controler would create a collection that contains an alternating list of
red, green that is the same size as the stuff collection. Call the attribute
tableclass.
$stuff, tableclass : { it, c |
<tr class="$c$">
<td>$it.firstName$</td>
<td>$it.lastName$</td>
<td>$it.address$</td>
</tr> }$
It is not necessary to actually store in memory the full tableclass as a
collection. Don't use ArrayList for example, create your own implementation
that just stores the things to alternate between and the number of items in
the virtual collection. Then provide a custom iterator.
I got this idea from your smarty example. I never heard of smarty before.
I'll go check it out.
Thanks,
-John
> -----Original Message-----
> From: stringtemplate-interest-bounces at antlr.org
> [mailto:stringtemplate-interest-bounces at antlr.org]On Behalf Of Nate
> Sent: Thursday, March 01, 2007 7:31 PM
> To: stringtemplate-interest at antlr.org
> Subject: [stringtemplate-interest] alternating row colows
>
>
> I know ST supports alternating templates, eg...
>
> $stuff:redRow(),greenRow()$
>
> However, I don't see this as being useful for rendering an HTML table
> with alternating row colors. Am I expected to do the following?
>
> $stuff:{
> <tr class="red">
> <td>$it.firstName$</td>
> <td>$it.lastName$</td>
> <td>$it.address$</td>
> </tr>
> },{
> <tr class="green">
> <td>$it.firstName$</td>
> <td>$it.lastName$</td>
> <td>$it.address$</td>
> </tr>
> }$
>
> I don't want to duplicate my template just to change the row color. I
> don't want to put my template in another file so it can be reused. I
> just want to know if the current row is even or odd. This is purely
> presentation logic, so it should be easy. Is there an easier way to
> accomplish this in ST?
>
> In Smarty it is done with something like this...
>
> <tr class="{cycle values="green,red"}">
> <td>$it.firstName$</td>
> <td>$it.lastName$</td>
> <td>$it.address$</td>
> </tr>
>
> -Nate
>
> _______________________________________________
> stringtemplate-interest mailing list
> stringtemplate-interest at antlr.org
> http://www.antlr.org:8080/mailman/listinfo/stringtemplate-interest
More information about the stringtemplate-interest
mailing list