[stringtemplate-interest] Odd even row formatting

Terence Parr parrt at cs.usfca.edu
Thu May 15 17:41:13 PDT 2008


Hi guys,Sorry for delays in responding to mail what I'm racing to get  
antlr v3.1 finished and grade my final exams... answers below. good  
questions!!

On May 15, 2008, at 4:09 PM, Trevor Griffiths wrote:

> Hi,
>
> 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:
>
> <tr>
>     <b><td>$it.subject$</td><td>$it.count$</td></b>
> </tr>
>
> And the evenRow template might be:
>
> <tr bgcolor= red>
>     <td>$it.subject$</td><td>$it.count$</td>
> </tr>
>
> The subjectList is iterated over and every even row will have a red  
> background.
>
> One problem I have with this approach is that it seems to violate  
> the DRY principle.

Well, as you say, this one is pretty simple, but the principle is  
correct. There will be repetition. In that case I suggest that you  
simply use another template to factor out there, and elements like

row2col(left,right,color) ::= ...

then do

oddRow() ::= "<row2col(a=it.subject,b=it.count)>"
evenRow() ::= << <row2col(a=it.subject,b=it.count, color=red)> >>

> 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$ >
>     <td>$it.subject$</td><td>$it.count$</td>
> </tr>

>
> 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 attributes..."

switch on iteration is something that is fine; the only problem is  
finding a construct that allows only that and not generic testing of  
integers etc. I wonder if something could be done with a map and using  
the index as keys or something.

> 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.

also, can use implicitly created aggregates

st.setAttribute("aRow.{odd,data}", false, mydataobject);

  this makes it much easier then you can simply test $it.odd$ and use  
$it.data$ to get your data element.

> 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 :-)

Not really in that the model can know whether something has an even or  
odd value ;)

> 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 :-)

Perhaps a switch or something I was limited to the iteration?

> 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.

yeah, we would have to hide it so did not look like a comparison ;)

Ter


More information about the stringtemplate-interest mailing list