freemarker.ext.format
Class MessageFormat

java.lang.Object
  extended by freemarker.ext.format.MessageFormat
All Implemented Interfaces:
TemplateMethodModel2, TemplateModel, java.io.Serializable

public class MessageFormat
extends java.lang.Object
implements TemplateMethodModel2, java.io.Serializable

Formats a message using a given substitution pattern. The arguments can be wrapped object or TemplateModel values.

Formats are specified using the same pattern as the java.text.MessageFormat class. As a performance optimization, some caching of pre-parsed format templates occurs.

Usage:
From java:

 TemplateModelRoot root = new SimpleHash();

 root.put( "formatMessage", freemarker.ext.format.MessageFormat.getInstance() );

 ...
 

From your FM-Classic template:

 The following creates a directory path:
 <assign name = "FM-Classic">
 <assign description = "template engine">
 <assign output = formatMessage( "{0} is a good {1}. {0} {2}!", name, description, "rocks" )>
 ${output}

 ...
 

Since:
1.9
Version:
$Id: MessageFormat.java 1067 2011-06-06 10:55:28Z run2000 $
Author:
Nicholas Cull
See Also:
Serialized Form

Method Summary
 TemplateModel exec(java.util.List<TemplateModel> arguments)
          Executes a method call.
static MessageFormat getInstance()
          Return a singleton instance of this class.
 boolean isEmpty()
          Is the object empty?
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getInstance

public static MessageFormat getInstance()
Return a singleton instance of this class.

Returns:
a new DateFormat object

isEmpty

public boolean isEmpty()
                throws TemplateModelException
Is the object empty?

Specified by:
isEmpty in interface TemplateModel
Returns:
true if this object is empty, otherwise false
Throws:
TemplateModelException

exec

public TemplateModel exec(java.util.List<TemplateModel> arguments)
                   throws TemplateModelException
Executes a method call. The first value is used as a pattern to a MessageFormat object for formatting the remaining arguments. The remaining arguments are unwrapped using defaultUnwrap before being supplied as arguments to the MessageFormat object.

Specified by:
exec in interface TemplateMethodModel2
Parameters:
arguments - a List of TemplateModel objects containing the values of the arguments passed to the method.
Returns:
the TemplateModel produced by the method, or null.
Throws:
TemplateModelException