freemarker.ext.misc
Class AsString

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

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

Performs a type conversion from a number model to a String model. This is useful when we need to perform String concatenation on two numbers, or when we only want a number from a multimodel.

Usage:
From java:

 TemplateModelRoot root = new SimpleHash();

 root.put( "asString", freemarker.ext.misc.AsString.getInstance() );

 ...
 

From your FM-Classic template:

 <assign var1 = 5>
 <assign var2 = 3>
 <assign var3 = asString( var1 ) + asString( var2 )>
 <p>The concatenation of ${var1} and ${var2} is: ${var3}</p>

 ...
 

Note:
AsString is a singleton instance. Use the getInstance() method to retrieve instances of this model.

Since:
1.8
Version:
$Id: AsString.java 1101 2013-04-01 04:17:32Z run2000 $
Author:
Nicholas Cull
See Also:
Serialized Form

Method Summary
 TemplateModel exec(java.util.List<TemplateModel> arguments)
          Perform a number to string conversion on the given template model.
static AsString getInstance()
          Retrieve a singleton instance of this class.
 boolean isEmpty()
          Is this model empty?
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getInstance

public static AsString getInstance()
Retrieve a singleton instance of this class. Since there is no state information held between calls, there is only a need for one instance.

Returns:
an instance of this AsString class

exec

public TemplateModel exec(java.util.List<TemplateModel> arguments)
                   throws TemplateModelException
Perform a number to string conversion on the given template model.

Specified by:
exec in interface TemplateMethodModel2
Parameters:
arguments - a List of exactly one template model to be converted
Returns:
a new TemplateScalarModel of the String value
Throws:
TemplateModelException - More than one argument is supplied, or the model is neither a number nor a string model.

isEmpty

public boolean isEmpty()
                throws TemplateModelException
Is this model empty?

Specified by:
isEmpty in interface TemplateModel
Returns:
false, to indicate the model is non-empty
Throws:
TemplateModelException