[stringtemplate-interest] alternating row colows

Nate misc at n4te.com
Thu Mar 1 18:54:18 PST 2007


Interesting solution, though I don't like that the controller has to 
step in for the presentation to perform "presentation only" logic.

IMO, named templates should go in a group file when multiple template 
files can use them. But if only a single template will use the named 
template, why should it be in a different file? It just makes following 
the template markup harder.

It would be great it I could do this...

$columns = {
    <td>$it.firstName$</td>
    <td>$it.firstName$</td>
    <td>$it.address$</td>
}$
$stuff:
    {<tr>$it:columns()$</tr>},
    {<tr class="odd">$it:columns()$</tr>}
$

Another use for declaring inline templates is that then you could pass a 
template to another. Eg, I recently tried this...

$outputTable(
    headers=["Name", "Address"],
    data=stuff,
    columnsTemplate={
        <td>$it.firstName$</td>
        <td>$it.firstName$</td>
        <td>$it.address$</td>
    }
)$

I believe this fails because the inline template is evaluated 
immediately and the result is passed as a value. Maybe this could be 
written like this...

$columnsTemplate={
    <td>$it.firstName$</td>
    <td>$it.firstName$</td>
    <td>$it.address$</td>
}$
$outputTable(
    headers=["Name", "Address"],
    data=stuff,
    columnsTemplate=columnsTemplate
)$

On a separate note, I tried this...

simpleTemplate (stuff) ::= <<
    first: $stuff:{$it$}$
    second: $stuff:{$it$}$
 >>

...

$simpleTemplate(stuff=[1,2,3,4])$

The output is...

first: 1234
second:

The reason seems to be that the inline list is turned into an iterator, 
so naturally the second time it is accessed hasNext() will be false. :(

-Nate


John Snyders wrote:
> 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


More information about the stringtemplate-interest mailing list