[stringtemplate-interest] selecting a dropdown option

John Snyders jjsnyders at rcn.com
Tue May 15 18:56:35 PDT 2007


How about parallel list iteration. I think I may have mentioned this 
technique in the past but in a different context.
I was going to say I didn't have time to try my solution out but then I 
remembered stst.
Putting stst 
(http://hardlikesoftware.com/weblog/2007/04/26/on-learning-stringtemplate/) 
to good use here is an example:

file listData.js is:
{locations: [
  { name: "France"},
  { name: "Canada"},
  { name: "USA"},
  { name: "England"},
  ],
 locsel: [ null, null, 1, null ]
}
file selectlist.st is:
<select name="location">
$locations,locsel:{location,sel|
    <option$if(sel)$selected$endif$>$location.name$</option>
}$
</select>

stst selectlist listData.js produces:
<select name="location">

    <option>France</option>

    <option>Canada</option>

    <option selected>USA</option>

    <option>England</option>

</select>

Back before objects were invented rather than have an array of objects 
with name and selected properties you would sometimes use parallel 
arrays. You may not have control over the locations collection but you 
can add your own listsel collection. To make it even better don't 
actually store an array of null references with one non null one. Just 
implement your own collection that knows which index is selected and 
implement an iterator that returns non null for just that index.

-John

Nate wrote:
> I have a template similar to this where I want to select one of the 
> locations...
>
> <select name="location">
> $locations:{location|
>     <option $if(location.name == 
> selectedLocation)$selected$endif$>$location.name$</option>
> }$
> </select>
>
> Obviously that isn't valid ST and would break separation. The location 
> objects are part of an object model I don't have access to, otherwise I 
> could add an "isSelected" method and use a template like this...
>
> <select name="location">
> $locations:{location|
>     <option $if(location.selected)$selected$endif$>$location.name$</option>
> }$
> </select>
>
> Is there any other way than to wrap the location objects in my own class 
> and have a whole slew of delegate methods? This is a common scenario in 
> my webapp.
>
> -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