[stringtemplate-interest] Renderer "modes"

Oliver Flege o.flege at market-maker.de
Thu Jul 6 00:23:27 PDT 2006


Hi,

I'm currently trying to switch our web frontends from Velocity to ST
and stumbled upon the 'Renderer "modes"' thread (mid June), because I also have
the need to support long and short forms for Date objects etc.

The recommendation back then was basically to subclass StringTemplate
as described in http://www.cs.usfca.edu/~parrt/papers/i18n.pdf
and to override the setAttribute method to add a wrapper object to the
model. For me, that's just not practical as it does not work for
attribute properties and it would be just too awkward to add each
property as a first-class attribute.

But I also agree with the comments in the mentioned thread that it does
not make sense to add the name of the format/rendering-method as a parameter
(e.g., ...;format="js")

Instead, I'd suggest the following:

1. define a group with required templates
  group foo;
  shortDate() ::= "<it>"
  longDate() ::= "<it>"

2. register custom attribute renderers for the _templates_:
  group.lookupTemplate("shortDate").registerRenderer(Date.class, new DateRenderer("MMM yy"));
  group.lookupTemplate("longDate").registerRenderer(Date.class, new DateRenderer("yyyy-MM-dd HH:mm:ss"));

3. apply templates:
    $d:shortDate()$          to format an attribute d using short format
    $bar.date:longDate()$    to format bar's date property using long format

To support this, it is at least necessary to
add the following line to StringTemplate.dup(...) (which is missing anyway):
         to.attributeRenderers = from.attributeRenderers;

In addition to that, it would be helpful to add methods to StringTemplateGroup to
register renderers for templates:
  registerRenderer(String templateName, Class c, Object o)
The group could then store this information and re-register the renderers whenever
it reloads the template (esp. during development).



Oliver



More information about the stringtemplate-interest mailing list