[stringtemplate-interest] Odd even row formatting

Trevor Griffiths tgriffiths at liveworld.com
Thu May 15 16:09:53 PDT 2008


The topic is how to change the style of rows in a table based on their  
odd or even index value.
Or more generally, given a set of ordered data to iterate over how to  
make the style dependent upon the data's position in the the set. That  
is on the index of the data.

The current ST suggestion (at least in the  documentation) is to set  
up this kind of template structure:

$subjectList: oddRow(), evenRow()$

Where the oddRow template might be:


And the evenRow template might be:

<tr bgcolor= red>

The subjectList is iterated over and every even row will have a red  

One problem I have with this approach is that it seems to violate the  
DRY principle.
The oddRow and evenRow have to be virtually copies of each other. I  
picked a simple row structure, it could well be a lot more complicated  
than this.

 From a separation of concerns perspective, is there any reason why I  
shouldn't be able to explicitly key off the index, either through an  
odd/even boolean of some sort? This would allow this kind of expression:

<tr $if (odd)$ bgcolor= red $endif$ >

And while odd/even is a fairly common requirement, it begs the general  
question of why can't I change the presentation of the data based on  
its index value? I'm not attempting to change the model. I am simply  
trying to: "operate on a set of incoming read-only data values, called  

Actually, I did just implement odd/even in the model by computing odd/ 
even as each subject line bean is loaded in to an array of beans. The  
additional subject line field, "odd," is empty if the respective line  
is even. (Each subject line bean has the attributes: subject, count,  
odd). The "odd" field amounts to meta data for the line's position in  
the array.

But, now don't  we have a case of the model entangled with the view?  
there seems something slightly awry here or is it just me :-)

Anyway, I'd be interested in your opinion on this topic:

So, in conclusion, Is it reasonable to have an odd/even attribute, if  
not the index, available in the presentation layer, or is this the  
first step on a slippery slope :-)

If the index is made available, then it would obviously need to be  
compared with some value, which breaks the simplicity of the "does it  
exist" rule.



-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org:8080/pipermail/stringtemplate-interest/attachments/20080515/2349ba67/attachment.html 

More information about the stringtemplate-interest mailing list