[stringtemplate-interest] Question about MVC separation for code generation.

Stenglein, David David.Stenglein at deshaw.com
Mon May 12 11:03:04 PDT 2008

Hello all,

I've been using StringTemplate for the last month or so to do some code
generation in python (I happy to finally get the chance to use it).

My question is a bit more generic in nature. What I am doing is building
a model out of a subset of XML Schema. My model has some classes to
represent complex and simple types, attributes and a couple of other
things from XML schema.

User = ComplexType(name="user")
User.addAttribute( Attribute(name="username",

My question regards where to put information about the "kind" of type
that is being passed to the template for a piece of the schema
(essentially a data structure class).

I started out with a binary system with either simple or complex types,
and I just set an attribute readable through iscomplex(). This has
worked so far, but now I want to treat certain things, like lists,
differently and subclassing my datatype representation and adding
is<blah> tests seems a bit redundant.

At the same time, my controller code is pretty simple (just iterate over
types and call a top level template), and I'm wondering if I should be
putting more logic in here. I was thinking of having the controller load
different templates based on class name, but that doesn't solve very
much, since I just have a simple top-level template "class(type) ::="
and within that there are statements that are conditional for
relationships to other types. Having the controller involved any further
in would seem intrusive and is potentially implementing some of the view
in the wrong place.

Another thought I've had is to change my model classes (e.g.
ComplexType, SimpleType, ListType) to have a method for returning the
name of the class (not the schema element) and have the templates do
includes based on this, rather than having the if statements at all.
Combined with some polymorphism, this could solve a fair bit of my

Out of this, I have a couple of questions:

  -Is my description of the problem totally confusing? I've tried to be
careful about using the terms model and 
   schema, but it seems too easy to switch them around. Is there
better/standard terminology to use?

  -When I have complete controll over the input model and output view,
is there much of any reason to put intelligence 
    in the controller, especially since I am doing a pretty
straightforward "transformation"? I am generating code code for
    two different cases and in the second case, things are a little more
complex which gives rise to this problem.

  -Are there any suggestions for handling this differently?


More information about the stringtemplate-interest mailing list