Overview
The format of interpolations is
${expression}
, where
expression
can be all
kind of expression (e.g. ${100 + x}
).
FreeMarker can be configured to use
[=expression]
syntax
instead. See more
about alternative syntaxes...
The interpolation is used to insert the value of the
expression
converted
to text (to string). Interpolations can be used only on two places:
in text sections (e.g.,
<h1>Hello ${name}!</h1>
) and in string literal
expressions (e.g., <#include
"/footer/${company}.html">
).
The result of the expression must be a string, number or date/time/date-time value, because (by default) only these types are converted to string by interpolation automatically. Values of other types (such as booleans, sequences) must be converted to string "manually" (see some advices later), or an error will stop the template processing.
It's a frequent mistake to use interpolations on places
where they needn't/shouldn't/can't be used. Interpolations work
only in text sections (e.g.
<h1>Hello ${name}!</h1>
) and in
string
literals (e.g. <#include
"/footer/${company}.html">
). A typical
WRONG usage is <#if
${big}>...</#if>
, which will give syntactical
error. You should simply write <#if
big>...</#if>
. Also, <#if
"${big}">...</#if>
is
WRONG, since it converts the parameter value
to string and the if
directive wants a boolean
value, so it will cause a runtime error.
Automatic escaping
If the interpolation is in a text section (not in a string literal expression), the text that it inserts goes through automatically escaping, if FreeMarker was properly configured. See more about escaping here....
Guide to inserting numerical values
If the expression evaluates to a number then the numerical
value will be converted to string according the default number
format. This may includes the maximum number of decimals, grouping,
and like. Usually the programmer should set the default number
format; the template author doesn't have to deal with it (but he can
with the number_format
setting; see in the documentation of
setting
directive). Also, you can override
the default number format for a single interpolation with the string
built-in.
The decimal separator used (and other such symbols, like the group separator) depends on the current locale (language, country), that also should be set by the programmer. For example, this template:
${1.5}
will print something like this if the current locale is English:
1.5
but if the current locale is German then it will print something like:
1,5
since German people use comma as decimal separator.
When the number you print will be read by some computer
process, always use the c
built-in, to get rid of any localized, or otherwise fancy
formatting! Like when you print a database record ID as the part
of an URL or as an invisible field value in a HTML form, or when
you print CSS/JavaScript numerical literals. Example:
<a href="/shop/productdetails?id=${product.id?c}">Details...</a>
That ?c
there formats the number with a
simple US format, which most computer processes will
understand.
Guide to inserting date/time/date-time values
If the expression evaluates to a date-like value then that
will be transformed to a text according to a default format. Usually
the programmer should set the default format; the template author
doesn't have to deal with it (but if you care, see the
date_format
, time_format
and
datetime_format
settings in the
documentation of the setting
directive). Also, you can override the default formatting for
a single interpolation with the string
built-in.
To display a date-like value as text, FreeMarker must know
which parts of it are in use, that is, if only the date part
(year, month, day), or only the time part (hour, minute, second,
millisecond), or both. Unfortunately, because of the technical
limitations of Java platform, for some variables it is not
possible to detect this automatically; ask the programmer if the
data-model contains such problematic variables. When it's not
possible to find out which parts of the date are in use, then you
must help FreeMarker with the date
,
time
and datetime
built-ins (like ${lastUpdated?datetime}
), or it
will stop with error.
Guide to inserting boolean values
By default an attempt to print boolean values with
interpolation causes an error and aborts template processing. For
example this will cause an error: ${a == 2}
and
will not print ''true'' or something like that. That's because
there's no universally useful way of representing booleans
(sometimes you want to print yes/no, sometimes enabled/disabled,
on/off, etc.).
However, you can convert booleans to strings with the ?string
built-in. For example, to print the value of the "married"
variable (assuming it's a boolean), you could write
${married?string("yes", "no")}
.
FreeMarker can be configured with a default boolean format
with the boolean_format
setting, then
${married}
and such will work. However, in most
applications it's not recommended, as boolean should be rendered
differently on different places, and leaving the formatting on the
default is possibly just an oversight and thus should generate
error.
When you want to generate JavaScript or other computer
language parts, then
${someBoolean?c}
("c" stands for computer audience) should be used to
print true/false. (Remember that ?c
was also used
to print numbers for computer audience.)
Exact conversion rules
For those who are interested, the exact rules of conversion from the expression value to string (which is then still subject to escaping) are these, in this order:
-
If the value is a number, then it is converted to string in the format specified with the
number_format
setting. So this usually formats for human audience, as opposed to computer audience. -
Else if the value is date, time or date-time, then it is converted to string in the format specified with the
date_format
,time_format
ordatetime_format
setting, respectively. If it can't be detected what kind of date-like value it is (date vs time vs date-time), an error will occur. -
Else if the value is a string, then there is no conversion.
-
Else if the engine is in classic compatibility mode:
-
If the value is a boolean, true values are converted to "true", false values are converted to an empty string.
-
If the expression is undefined (
null
or a variable is undefined), it is converted to an empty string. -
Else an error will abort the template processing.
-
-
Else an error will abort the template processing.