Hi. I am new to this group and I am just learning about Velocity and
WebMacro and others. I hope the "[PROPOSAL]" heading is correct as I
have seen others use. I have not posted but once or twice. After reading
though the user and developer's guides and the VTL reference, I was
wondering if anyone has thought of including... 

(1) Providing a way to apply localized formatting to the value returned
by a reference and/or
(2) A way to extend the template language through events using the new
event mechanism.

Again, maybe this has already come up in the past or I'm missing some
concept (there's a lot to learn) but combining templates with some of
the concepts in the java.text.MessageFormat class seems like a good
marriage to allow template authors the ability to provide localized and
formatted display of their references.

Of course, for many values returned from references, the value needs no
adjustment. But in the event you are displaying currencies or date
values, the locale has quite a significant impact on the toString()
method.

For example, let's say the I have a variable named $now that returns a
reference to a java.util.Date object that I would like to display using
a special format. By allowing some optional formatting arguments to the
reference (example: ${now,date,short} and assuming a default locale
representing "en_US", the display would be "6/18/01". Of course, a
different default locale might produce a different result. By also
allowing a locale value to be assigned to the evaluation of the template
(maybe as an optional argument to the merge and/or evaluate methods)
this would allow for great flexibility in the display formatting of
references. Since it is optional in all of these cases, it would also be
backward compatible with the existing syntax. Well...I hope you get the
gist of what I'm looking for and/or proposing.

Even the locale value could be set at the engine, parse() or expression
level to allow very flexible formatting and hierarchy. For example:
${now,date,short,fr} to format a date in French with the short format.

Currently, I'm providing my own formatting object in the context that is
used to format the results of other date or currency expressions:

        Examples: $format.date( $now, "short" )

The $format object may be assigned a locale and time zone value for many
different types of formatting (from many of the java.text.* classes)

Also, if an event handler (very similar to the new
ReferenceInsertionEventHandler) was allowed to look ahead during the
parsing of the references, then a custom extension to the reference
could also be applied without even adding this to the official syntax of
the language. For example, if portion of the reference following the
variable, method and/or property was provided to the event handler along
with the reference to the evaluated reference expression, then a
Velocity developer could extend this as a custom extension without
changing the code of the base product and provide custom formatting and
special features (another example: a default values mechanism if the
return value is null or empty). This might be something like:

        public Object ReferenceInsertionEventHandlerEx( String
reference, Object value, String extraStuff ) throws ParsingException

Using the example syntax above, the parameters might supply the values:
reference = "${now", value=(a java.util.Date reference) and the
extraStuff=",date,short"

The event handler would have to interpret the extraStuff and use it to
return whatever is intended by adding this handler to the engine. But
there may be a better way to do this by supply a reference to the
InputStream to allow the developer to parse the stream following the
reference.

Anyway, I thought by sharing these thoughts that, if warranted, a dialog
could be establish if others find something like this useful. There
probably is a better way and having more brain power to examine the idea
would allow it to blossom or wither away.

Bill Boland
[EMAIL PROTECTED]

Reply via email to