Poor formatting configuration
Generates text that depends on changing data (like dynamic HTML).
Brought to you by:
revusky
Issues:
* Limited to global format or page specific format;
* Cannot access Format object from out side of
freemarker for parsing;
* Cannot have localized formats while using same template;
The role of localization is different from the role of
layout presentation. Need to have formatting
independent of template. Suggest to have a resource
bundle of format information that is accessible to
freemarker and the application that uses it.
Patch retrives format objects from resource bundle.
Logged In: YES
user_id=33187
I'm not 100% sure what the problem is.
We have a <#setting date_format=...> directive that allows
you to set a date format or number format or whatever for
whatever part of the template you want. Offhand I don't know
what you could need to do that is not possible using this
disposition.
I am not sure whether it is that you are unaware of this
<#setting...> directive or whether it is that this
disposition does not give you what you want. Basically,
you'll have to be a bit clearer about this issue.
Logged In: YES
user_id=33187
Hold on, I didn't see that you actually posted a patch. I'll
have to look at that and get back to you. ATM, I am still
unsure what it is you want to do that you can't do with the
current setup.
Logged In: YES
user_id=1444713
My templates are share for all locales. I have only one
copy of each template for every locale. I need different
formatting for different locales. The problem with the
#setting directive is that you have to include it for each
template and it can become difficult to manage for lots of
different locales across different templates. It would be
easier if all the formatting was configured in a resource
bundle or properties file.
I think it would also help separate the formatting from
the presentation layer. The people creating the template
don't know what the formatting is going to be and the people
deciding on the format don't know what the template system
is or how to setup a directive.
It is also important to ensure the parsing operations
(outside of freemarker) use the same format as freemarker.
By Using format directives it is not clear that both are
using the same format. If the formats are stored in a java
resource bundle, any application can have access to the
formats that are used in freemarker.
The patch I uploaded includes a FormatBundle class that
wraps a ResourceBundle providing conversion from String to
Format, for the built-in formats, when using a properties
files. It can also be used to wrap a Java ResourceBundle
for application that have special needs for loading formats
(like from a database).
Ticket moved from /p/freemarker/bugs/115/
As of 2.3.24, you can define custom formats (
TemplateNumberFormatFactory
orTemplateDateFormatFactory
technically) in theConfiguration
(or on theTemplate
orEnvironment
level), each having an unique name, that you can refer to asn?string.@foo
. Also, there's two custom formats shipped with FM,AliasTemplateNumberFormatFactory
andAliasTemplateDateFormatFactory
. With these you can give a "logical" name to another format, even to different formats per locale. So that does something like that was requested here. You write${n?string.@price}
, and what format that actually means is specified outside the template, and can depend on the locale.Last edit: Dániel Dékány 2015-09-16
FreeMarker 2.3.24 has a more flexible formatter setup, which, among others, allows you to use "symbolical" patterns, like
"@price"
, whose definition can be locale dependent, or actually can come from anywhere (by implementing custom formatters).Last edit: Dániel Dékány 2016-05-21