Monday, October 5, 2009, 11:29:46 AM, Prinz, Tobias wrote:
> Hi there,
>
> I got a set of pretty simple macros for printing out dates. One for
> giving only the day, one for the time, one for timezone. These all
> work fine, using the built-in features like ?string("dddd-mm-yy").
>
> Now I try (for debugging purposes) to write one that gives me the
> milliseconds since 1970 (which is not covered by built-in functions).
>
> For a java.util.Date, this is simple, right?
>
> wrapper.getMyDate.getTime()
>
> So I tried a naive approach in Freemarker:
>
> wrapper.myDate.time
>
> Sadly, this makes the system cry out:
> Expected hash. wrapper.myDate evaluated instead to freemarker.template.SimpleDate
>
>
> I wonder why that is happening.
> My assumption, though I did not find anything in the docs about it:
> There are obviously classes that get treated differently by
> FreeMarker. Some are just wrapped, but others get special features.
> java.util.Date is wrapped into freemarker.template.SimpleDate. The
> latter misses the former's getTime() method. Right?
The thing is, originally FreeMarker didn't exposed methods and
JavaBean properties at all. Now it has the BeansWrapper that does, but
for backward compatibility the DefaultObjectWrapper is used which
still doesn't expose the method/properties for "well-known" classes
like Date.
> If so: How can I prevent this from happening?
Use plain BeansWrapper... where you configure FreeMarker add this:
BeansWrapper wrapper = new BeansWrapper();
wrapper.setSimpleMapWrapper(true);
cfg.setObjectWrapper(wrapper);
Where cfg is the Configuration objet. Now, if you do it on a big
complex system, you better do some testing, because the template will
see the data-model a bit differently now (like you can write
${myDate.time}). It mostly shouldn't break anything, but who knows...
If this you find too risky, you can write a TemplateMethodModelEx that
extract the millisecond part.
Also, I think I will add a built-in for this in 2.3.16... it seems
it's often needed. myDate?time_millis or something. I think 2.3.16
will be released within two weeks or so, but I can add this built-in
much earlier in the nightly builds if you would use that. Would you?
> If not: How else does it work, what do I need to do?
>
> Thanks in advance,
> Tobias
--
Best regards,
Daniel Dekany
------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user