![]() |
||
|
||
![]() |
You use interpolation to insert values into the output. There are two types of interpolations:
Universal interpolation: ${expression}
Numerical interpolation: #{expression} or #{expression; format}
A frequent mistake of users is the usage of interpolations in places where it 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 bad usage is <#if ${isBig}>Wow!</#if>, which is syntactically WRONG. You should simply write <#if isBig>Wow!</#if>. Also, <#if "${isBig}">Wow!</#if> is WRONG too, since the parameter value will be a string, and the if directive wants a boolean value, so it will cause a runtime error.
If the expression evaluates to a string then it will be simply printed to the output.
If the expression evaluates to a number then the numerical value will be transformed to a text according to a default format. Usually the programmer should set the default format; you don't have to deal with it (but if you care, see the number_format setting in the documentation of setting directive).
![]() | ![]() | ![]() | |
![]() |
| ![]() | |
![]() | ![]() | ![]() |
will print this if the language of output is English:
![]() | ![]() | ![]() | |
![]() |
| ![]() | |
![]() | ![]() | ![]() |
but it will print this if the language of output is Hungarian:
![]() | ![]() | ![]() | |
![]() |
| ![]() | |
![]() | ![]() | ![]() |
since Hungarian people use the comma as the decimal separator.
You can modify the formatting for a single interpolation with the string built-in.
If the expression evaluates to a date then the numerical value will be transformed to a text according to a default format. Usually the programmer should set the default format; you don'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).
You can modify the formatting for a single interpolation with the string built-in.
To display a date as text, FreeMarker must know which parts of the date 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. If it is 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, or it will stop with error.
An attempt to print boolean values with universal 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.
However, you can convert booleans to strings with the string built-in.
You can do more fine-gradient formatting with the string built-in and with the number_format setting even now. Numerical interpolation will be deprecated and kept for backward compatibility only when we introduce a terser syntax for using string built-in.
With numerical interpolations the value must be numerical or an error aborts template processing. The advantages over universal interpolations are:
You can specify number formatting in the interpolation (see later).
Readability is better because it is visible that you want to print a number. Also, it will catch the error when you accidentally get a string where you should get a number.
So what is number formatting? As you have seen the second syntax of numerical insertion is: #{expression; format}. I show the usage of format by example:
Specify that the length of decimal fraction is always exactly 1: m1
Specify the maximum length of decimal fraction is 3, but it will use as few decimal digits as possible: M3
Specify the minimal length of decimal fraction is 1, but it can extend up to 3 digits when required: m1M3
For example, assume that x is 2.582 and y is 4:
![]() | ![]() | ![]() | |
![]() |
| ![]() | |
![]() | ![]() | ![]() |
![]() |
||
|
||
![]() |
![]() |
|
Page generated: 2004-06-15 22:17:59 GMT | FreeMarker Manual -- For FreeMarker 2.3 |