Re: how to make freeMarker simply call toString?

Daniel Dekany <[email protected]> Mon, 31 Mar 2014 23:59:18 +0200
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
Monday, March 31, 2014, 9:34:34 AM, Vitaliy Semochkin wrote:

> Thank you for fast reply, Daniel!
>
> I'm already digging in BeansWrapper
> am I right that in I want objects that do not override toString
> methods return a "¿", i need to provide a StringModel for them 
> instantiated as  modelCache.getInstance(object, StringModel.FACTORY)?

Most certainly you should override BeansWrapper.getModelFactory. And
then, instead of MapModel, CollectionModel, etc, you had to create
sub-classes of those where getAsString (inherited from StringModel) is
overridden.

Of course, it's quite awkward that you can't just override getAsString
in StringModel, but that's just one of the many cases where
inheritance is not flexible enough. I wonder if
StringModel.gasAsString should call back into BeansWrapper so that
there's a single point where you can control its behavior. Then you
don't even need to override getModelFactory. I just wonder if it's
useful enough in general to introduce the (small) overhead that the
indirection means.

> Do I need to override public Object unwrap(TemplateModel model, Class hint)?

Nope.

-- 
Thanks,
 Daniel Dekany


------------------------------------------------------------------------------