[stringtemplate-interest] Model/view separation and equality testing against literals...

Terence Parr parrt at cs.usfca.edu
Thu Oct 25 17:25:01 PDT 2007


On Oct 26, 2007, at 1:18 AM, Steven Atkinson wrote:

> Folks,
>
>  I am trying to move people from a ‘home-grown’ tag language to  
> StringTemplate.

Hooray!
> The home grown language has a very popular equality-test-against-a- 
> literal feature:
>
>
>
> “ At Halloween we have a large pumpkin $if(pumpkinCount ==1)$ patch  
> $else$ farm $endif$ in our yard. “

uh oh. ;)  model-based login in the view. ;)
>  (I think they also have ‘<’, ‘>’ operators.)
>
>  So far as I currently understand it, the “separate model/view” way  
> around the need to compare literals is to make a view property  
> reference to a model method that does the comparison, right?

Correct!
>  e.g.
>
>  // Model object
>
> Class Pumpkin {
>
>    int pumpkinCount;
>
>
>
>    Boolean isSinglePumpkin();
>
>    Boolean isMultiplePumpkins();
>
> }
>
>  Now we can say:
>
> “ At Halloween we have a large pumpkin $if(pumpkin.isSinglePumpkin) 
> $ patch $else$ farm $endif$ in our yard. “

yep.  otherwise people could do "if user=='parrt'" when they should  
ask "isSuperUser"

> Now that’s fine, and it would be nice to standardize these things  
> into “pumpkin” model objects.
>
> Even when I need to do different things depending on if I have  
> 1-50, 51-100, 100-500, 500+ pumpkins….then I can just add more  
> methods to deal with each band….
>
>  But – it imposes more work on the modeler to provide the right  
> properties, and imposes on the template author to learn the  
> properties.
>
> I guess these are both small prices to pay…I know it will feel very  
> limiting to my template authors who are used to the equality tests…
>
>  My question is – am I missing an easy way to conditionally branch  
> on an attribute’s value using StringTemplate?
Nope. ;)  BUT, you can standardize things.  you can override  
StringTemplate.setAttribute() and auto-wrap all integers and strings  
and whatever else with formatter/renderer objects that add standard  
properties.  Then, the template designers can ask about things.

The one issue I'd bring up is that methinks there is a higher level  
concept to test rather than simple integer compare.  For example, do  
you mean "bp<120" or do you mean "bloodPressureIsOk"?  The higher the  
level, the better as it isolates you from change when doctors lower  
the min ok blood pressure, the bastards! ;)
>  (Oh yeah, I have built a  variation on the indirect-property-value- 
> equality-map hack at http://opensource.atlassian.com/projects/ 
> spring/browse/SPR-3266 but that only works for non-literals, unless  
> you add literals as attributes to a map, and it feels and looks  
> really awful :  $if(equals.(pumpkinCount).(“1”)$   with map.put 
> (“1”, Integer.valueOf(1)) in the attributes… yuck)
Yeah, that works and is probably defeating the separation ;)

Ter



More information about the stringtemplate-interest mailing list