[stringtemplate-interest] Managing "type specific" subtemplates

Nathan Ward nward at resqsoft.com
Mon Dec 1 12:45:25 PST 2008


Any suggestions for how to handle case where a different subtemplate should
be used depending on the type of a given model object? Is it reasonable to
use a Map to determine the subtemplate to use for this type of situation? 
 
For example, I'm generating JSPs and I have a Screen object that has a list
of ScreenElement objects. There is an inheritance hierarchy of
ScreenElements. So, there is a Textbox class that inherits from
ScreenElement as well as a DropDownList, Checkbox, etc. So, my jsp template
needs to use a different subtemplate based on the type of the ScreenElement.
My current solution (which is working) is use a map to specify the
subtemplate to be used and to define a ScreenElementType attribute on each
ScreenElement subclass so that I can lookup the subtemplate by
ScreenElementType. 
 
  e.g. jsp(screen) ::= <<
            $screen.screenElements: {screenElement|
$screenElementTypeMap.(screenElement.type.name)$}$$
  >>
 
textboxTag ::= << 
   <html-el:text...
>>
 
screenElementTypeMap :: [
    "textbox" : "textboxTag",
    "checkbox" : "checkboxTag"
]
 
This is not exactly my actual template code and there are several other
cases like this. In some cases, I only one ScreenElement type has a
subtemplate. I use a map in that case, but just specify a subtemplate for
only the type that I'm interested in. In other words, I'm using a map to
filter the list of ScreenElements in this case.
 
Nathan Ward
ResQSoft, Inc.
703.861.9103
www.resqsoft.com
that 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org:8080/pipermail/stringtemplate-interest/attachments/20081201/63000ff9/attachment.html 


More information about the stringtemplate-interest mailing list