#-#
#-# Directives
#-#
#-# Strings
Directive Description
boolean Converts string to boolean (true, false).
cap_first Capitalizes first word of string.
capitalize Capitalizes every word in string properly.
chop_linebreak Removes line-break from end of string.
contains (substring) Checks if string contains substring.
date, time, datetime Converts string to date/time/datetime based on *_format.
ends_with (substring) Checks if string ends with substring.
ensure_ends_with (substring) Returns string with substring appended if not already there.
ensure_starts_with (substring) Same as ensure_ends_with, but prepended.
groups[x] Returns the returns the regex group from the matches method if it exists.
html Returns the string with html characters parsed to ampersand codes.
index_of (substring) Returns the index of the requested substring in string.
j_string Escapes the string for Java String Literals.
js_string Escapes the string for Javascript String Literals.
json_string Escapes the string for JSON String Literals.
keep_after (substring, flags="") Returns all text after substring, not including substring.
keep_before (substring, flags="") Returns all text before substring, not including substring.
last_index_of (substring) Returns the index of the last occurance of substring.
left_pad (x, seperator=" “) Returns the string with [seperator] prepended up to a maximum length.
length The number of characters in the string.
lower_case Returns string in all lower case.
matches Regex Pattern checker.
number The string as a number.
replace (substring, replace) Returns all occurances of substring with replace.
right_pad (x, seperator=” “) Same as left_pad, but appended instead.
remove_beginning (substring) Returns string with substring removed from the beginning.
remove_ending Same as remove_beginning, except fromt he ending.
rtf Rich Text Format.
split (substring) Split a string at each substring.
starts_with Same as ends_with, but for string beginning.
string Takes a multi-value variable and returns just the string parts.
substring Deprecated, use string[x..y].
trim Removes leading and trailing whitespace.
uncap_first The opposite of cap_first.
upper_case Opposite of lower_case.
url URL Character escaping.
url_path Same as url but doesn’t escape slashes.
word_list Returns a sequence of all words in a string.
xhtml Same as html, but for xhtml escaping.
xml Same as hxtml, but different code for ‘.
#-# Numbers
Directive Description
abs Absolute Value.
c Num to String without formatting.
is_infinite True if number is floating point infinite.
is_nan True if number is NaN.
round, floor, ceiling Rounds to closest, lowest, and highest, respectively.
string.format Num to String with optional formatting (number|currency|percent|computer).
#-# Date / Time
Directive Description
date Returns date only from a datetime value.
time Returns time only from a datetime value.
datetime Returns date and time from a datetime value.
(date/time/datetime)if_unknown Marks a date-like value with specified missing subtype.
iso… Converts date-like value to specified iso format.
string.format Converts date-like object to string. See freemarker manual for formats.
#-# Booleans
Directive Description
c Boolean to String for computer languages.
string(“str1”, “str2”) Boolean to String with human-readable flags (“str1” = true and “str2” = false).
#-# Sequences
Directive Description
chunk(x, filler=null Splits sequence into multiple sequences of size x. Last chunk.
first Returns the first element of the sequence.
join(seperator) Concatonates sequence elements with seperator in between.
last Returns the last element in the sequence.
reverse Returns the sequence in reverse order.
seq_contains(needle) Returns true if the squence contains needle.
seq_index_of(needle) Same as contains, but returns the index of the first element.
seq_last_index_of(needle)
size
sort
sort_by
#-# Nodes (XML)
Directive Description
ancestors(name) Returns a sequence of nodes ancestors.
children Returns a sequence of all child nodes.
node_name Returns the node name (directive invoked when node is “visited”).
node_namespace Returns namespace string of node.
node_type Returns a string with the node type.
parent Returns the immediate parent node.
root Returns the root node.
#-# Hashes
Directive Description
Keys A sequence with all the hash keys.
Values A sequence with all the hash values.
#-# Misc
Directive Description
byte,double,float,int,long,short Converts numbers to other types of numbers.
eval Evaluate a string as an FTL expression.
has_content Returns true if variable exists and isn’t empty, false otherwise.
interpret Returns the value of a string interpreted as FTL code. Sort of like a function.
is_…[type] Checks if variable is of type [type]. See docs for full list.
namespace Returns macro or function namespace, if any.
new Creates a variable of type class. Usage is var = “FQDN”?new(options).
number_to_[date/time/datetime] Converts a number to date/time/datetime, using the number of seconds since epoch.
#-#
#-# Directives reference
#-#
Directive Description
assign name=value [in nshash] Creates a variable called name.
attempt/recover try/catch.
compress Removes all but one newline character between lines.
escape x as x?[escape type] <pre> tag for freemarker code.
flush Forces generated output to be written. 99% of the time is not neccessary.
ftl Tells programs that file is an ftl file.
function/return Similar to a macro, except returns a specific value as denoted by return.
global Makes a global variable. Variable will be exposed as if at data-model level.
if/elseif/else Creates an if/elseif/else block.
import path as hash Imports the library at location path for use in the current ftl file.
include path Includes the file at location path for use in current document.
list sequence as item foreach loop.
local Like assign, but creates local variables within macros or functions.
macro/nested/return Macro outputs a block of code which wraps nested, return works as a break.
noparse <pre> tag for ftl.
nt No trim. Disables whitespace stripping.
setting name=value Modifies parse settings. View full list in the docs page.
stop [reason] Per ftl docs: an emergency brake. Returns reason if set.
switch/case/default/break A regular switch block.
t, lt, rt Trim, left trim, right trim. Ignores whitespace based on tag position used.
visit/recurse/fallback Used for parsing trees, usually XML. See the ftl docs for full info.
#-#
#-# Examples
#-#
#-# Include
<#include “/copyright_footer.html”>
#-# Assignment
Any object except null can be passed to a variable.
<#assign countries = model.countries>
<#assign myString = “String data”>
#-# If / elseif / else
Common operators are supported
&&, ||, !, ==, !=, >, <, >=, <=
Simple if condition
<#if user == “jdoe”>
Hi John Doe!
</#if>
Nested if condition
<#if x == 1>
…
<#elseif myVar?has_content>
…
<#else>
…
</#if>
#-# Iterate array
<#list [“red”, “green”, “blue”] as color>
${color}
</#list>
<#list countries as country>
${country_index + 1}. ${country}
</#list>
#-# Iterate over hashmap keys
<#list capitalList?keys as key>
${key} = ${capitalList[key]}
</#list>
<#assign h = {“name”:“mouse”, “price”:50}>
<#assign keys = h?keys>
<#list keys as key>
${key} = ${h[key]};
</#list>
#-# Dealing with missing variables
A non-existent variable and a variable with null value is the same
<#if model.object.user??>Welcome ${user}</#if>
<#if model.object.user?has_content>Welcome ${user}</#if>
If the value is null/empty, replace with a default value
${user!“Anonymous”}
${animals.python.price!0}
#-# Methods and functions
The average of 3 and 5
${avg(3, 5)}
The average of the price of a python and an elephant
${avg(animals.python.price, animals.elephant.price)}
Arithmetical calculations
(x * 1.5 + 10) / 2 - y % 100
Comparison
x == y, x != y, x < y, x > y, x >= y, x <= y
Logical operations
!registered && (firstVisit || fromEurope)
#-# Call Java Method
${model.name}
${model.getName()}
#-#
#-# References
#-#