StringTemplate

By james

I may not have emphasized enough in the previous post how much I really like what Terrence Parr is doing with antlr and StringTemplate. This article (especially the linked article on Enforcing Strict Model View Separation in Template Engines) really made me realize some bad things about the template engine I was starting to write (specifically around the conditionals). So, now the conditionals look something like this:

#user_name .boss:not($user.boss){
display:remove;
}
#user_name .boss{
content:$boss_salutation;
}
#favorite_song:if($user.favoriteSong){
content: $user.favoriteSong;
}
#favorite_song:not($user.favoriteSong){
content: $unknown_song;
}
/* shorthand for the above 2 selectors…if first arg is empty, second is used as default */
#favorite_song{
content: $user.favoriteSong, $unknown_song;
}

And the StringTemplate actually has me wondering whether I should write a new one at all. I think Stitch (note to self…probably should get a name…ctrlTemplate?) still adds some value over what StringTemplate does, by fully separating the markup from the rules about how the data is to be inserted into the document…and then the rules file being in a format that is VERY similar to css, a format most web designers should be very familiar with. Then the markup can change quite drastically so long as the ‘css’ rules still match (mostly ids and class selectors), and the rule file need not change. I do think that if/when the tempates have a server-side ‘compiled’ version, its very possible it will use StringTemplate.

I was thinking that the ctrlTemplate could also potentially operate on tree structures too (not just xml/html/xhtml). Like, for example, an AST tree result from a parser of some sort. Just a thought.

Leave a Reply