11.4.3 format() function
The format function takes a variable number of arguments:
- the first argument is the "format string". This is a string containing placeholder like {0} where content should be inserted. To those familiar with that, it is similar to Java's MessageFormat, but not the same.
- the other arguments are the parameters to be inserted into the first format string
11.4.3.1 Format string syntax
11.4.3.1.1 Basics
The format string contains placeholders using {...} syntax. If you want to insert {...} literally, escape it using backslash: \{...}.
The placeholder contains the index of the parameter to insert, and optionally some formatting arguments.
A simple format string thus might look like:
The quick {0} fox jumps over the {1} dog.
Used with the format function in the template language:
${format('The quick {0} fox jumps over the {1} dog.', 'brown', 'lazy')}
If the first argument would be part of a resource bundle below the key 'quick_dog_phrase', the usage would be:
${format(i18n('quick_dog_phrase'), 'brown', 'lazy')}
11.4.3.1.2 Formatting arguments
Formatting arguments are useful when the value to insert is not a string, but a date or number.
The full format of the placeholders is:
{index,type,style_or_pattern}
The parts are:
- index: 0, 1, ... as explained before
- type: how to format the object
- style_or_pattern: some types take an extra style hint or formatting pattern as an argument
Dates will be adjusted to the user's timezone when formatting.
The table below lists the available types and styles.
|
Object type |
Type |
Style / pattern |
|---|---|---|
|
dates (java.util.Date |
date |
none / S / M / L / F (short/medium/long/full) |
|
time |
none / S / M / L / F |
|
|
datetime |
none / two characters from S / M / L / F / - |
|
|
dtpattern |
A joda-time date/time formatting pattern. |
|
|
numbers (java.lang.Number) |
int |
none |
|
nrpattern |
A Java SimpleNumberFormat pattern. |
11.4.3.1.3 Examples
Format a single date:
${format("{0,date}", someDateObject)}
Format a single date using a pattern:
${format("{0,dtpattern,dd/MM/yyyy}", someDateObject)}
Format a date-time using long date format and small time format:
${format("{0,datetime,LS}", someDateObject)}
Previous