Re: Number types with custom formatting

Daniel Dekany <[email protected]> Sat, 6 Apr 2013 21:01:42 +0200
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
Friday, April 5, 2013, 12:10:21 AM, Smerek, Martin wrote:

> Hi all,
>
> We'd like to use FreeMarker for our project. We have special
> formatting for different number types (internal id, multiple
> currencies, quantity) and we'd like to offer our users a comfort of
> automatic formatting without making them to call build-ins, e.g. use simply
>
> ${transaction.total}
> ${transaction.quantity}
>
> instead of
>
> ${transaction.total?string.currency}
> ${transaction.quantity?string.number}
>
> We tried to implement both TemplateNumberModel and
> TemplateScalarModel interfaces, but in
> freemarker.core.Expression.getStringValue(TemplateModel, Expression,
> Environment), TemplateNumberModel always takes priority before
> TemplateScalarModel. This results in one global format for all number types.
>
> Quick and dirty solution is to move the above mentioned condition
> with TemplateScalarModel before the one with TemplateNumberModel.
> However, we aren't sure why the sequence of the conditions looks as
> it does and if the change wouldn't cause some other problem. Any
> thoughts on this?

I don't know why the order is like this (I don't remember, if I was
here at all) but I think it's good as it is. TemplateModel-s meant to
deal with the MVC "M" part, not with "V", and it's the template (and
partially the FreeMarker settings) meant to control how are things
rendered. That practically means that some models may give string
values that are just last-resort values as far as displaying goes,
like toString() return values. It would be quite annoying/confusing if
the template uses that ignoring the number_format and date_format
settings and the current locale and time-zone. But I understand where
are you coming from, but see that later.

Regarding what will break if for now you just reorder those
conditions... If you are only using stock object-wrappings plus your
wrappings (which are designed to do the right thing when
TemplateScalarModel takes precedence), and you aren't using Jython or
Rhion, I don't think you will run into problems. That is, until
someone at the company later upgrades FreeMarker and thus breaks
things, or want to upgrade FreeMarker but won't because he has no
time/courage for re-patching it.

> Is there any way how to treat a number as number (to support
> comparison and arithmetics) but format it in completely custom way?

Nope... I think, the best would be if you can attach this
meta-information, the application domain type, to the number (or date)
value, and then, you can register formatters for application domain
types in the FreeMarker settings. So the model says "this number is a
price", and the FreeMarker settings say "format prices like this".

For this I need to add some new classes/interfaces to freemarker.template:

   class ApplicationDomainType(final String name)

and

  public interface ApplicationDomainTypeAwareTemplateModel extends TemplateModel {
      ApplicationDomainType getApplicationDomainType();
  }

and

  interface ApplicationDomainTypeFormatter {
      String format(
            ApplicationDomainTypeAwareTemplateModel value,
            Environment env,  // so you can get the locale, time-zone, etc.
            TemplateModel format  // used only for val?string(format)
            )
      throws TemplateModelException;
  }

and of course I have to add this to Configurable:

  addApplicationDomainTypeFormatter(ApplicationDomainType, ApplicationDomainTypeFormatter)
  ... etc.

Would that solve the problem? Any opinions?

Then, there's the question, when can I add this. I have a list of
things that must be done for 2.3.19, like even important bugfixes, but
I don't have much time in the near future. So, certainly 2.3.19 will
be released around the end of May, and I think I can fit this in.
That's almost 2 months away.

-- 
Thanks,
 Daniel Dekany


> Thank you very much,
> Martin Smerek
>
>
> NOTICE: This email and any attachments may contain confidential and
> proprietary information of NetSuite Inc. and is for the sole use of
> the intended recipient for the stated purpose. Any improper use or
> distribution is prohibited. If you are not the intended recipient,
> please notify the sender; do not review, copy or distribute; and
> promptly delete or destroy all transmitted information. Please note
> that all communications and information transmitted through this
> email system may be monitored by NetSuite or its agents and that all
> incoming email is automatically scanned by a third party spam and filtering service.


------------------------------------------------------------------------------
Minimize network downtime and maximize team effectiveness.
Reduce network management and security costs.Learn how to hire 
the most talented Cisco Certified professionals. Visit the 
Employer Resources Portal
http://www.cisco.com/web/learning/employer_resources/index.html