Show Menu

Freemarker Directives Cheat Sheet by

For freemarker, a Java templating language.
java     freemarker     templating

Built In References - Strings

boolean
Converts string to boolean (true, false)
cap_first
Capita­lizes first word of string
capitalize
Capita­lizes every word in string properly
chop_l­ine­break
Removes line-break from end of string
contains (subst­ring)
Checks if string contains substring
date, time, datetime
Converts string to date/t­ime­/da­tetime based on *_format
ends_with (subst­ring)
Checks if string ends with substring
ensure­_en­ds_with (subst­ring)
Returns string with substring appended if not already there
ensure­_st­art­s_with (subst­ring)
Same as ensure­_en­ds_­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 (subst­ring)
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_s­tring
Escapes the string for JSON String Literals
keep_after (subst­ring, flags=­"­")
Returns all text after substring, not including substring
keep_b­efore (subst­ring, flags=­"­")
Returns all text before substring, not including substring
last_i­ndex_of (subst­ring)
Returns the index of the last occurance of substring
left_pad (x, sepera­tor­=" ")
Returns the string with [seper­ator] 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 (subst­ring, replace)
Returns all occurances of substring with replace
right_pad (x, sepera­tor­=" ")
Same as left_pad, but appended instead
remove­_be­ginning (subst­ring)
Returns string with substring removed from the beginning
remove­_ending
Same as remove­_be­gin­ning, except fromt he ending
rtf
Rich Text Format
split (subst­ring)
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
Depric­ated, 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 difference code for '
 

Built In References - Booleans

c
Boolean to String for computer languages
string­("st­rin­g1", "­str­ing­2")
Boolean to String with human-­rea­dable flags. Provided as "­str­ing­1" for true and "­str­ing­2" for false

Built In References - Sequences

chunk(x, filler­=null)
Splits sequence into multiple sequences of size x. Last chunk
first
Returns the first element of the sequence
join(s­epe­rator)
Concat­onates sequence elements with seperator in between
last
Returns the last element in the sequence
reverse
Returns the sequence in reverse order
seq_co­nta­ins­(ne­edle)
Returns true if the squence contains needle
seq_in­dex­_of­(ne­edle)
Same as contains, but returns the index of the first element
seq_la­st_­ind­ex_­of(­needle)
size
sort
sort_by

Built In References - Nodes (XML)

ancest­ors­(name)
Returns a sequence of nodes ancestors. Starts with immediate parent and ends with root. Should be filtered with name
children
Returns a sequence of all child nodes
node_name
Returns the node name (directive invoked when node is "­vis­ite­d")
node_n­ame­space
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

Built In References - Numbers

abs
Absolute Value
c
Num to String without formatting
is_inf­inite
True if number is floating point infinite
is_nan
True if number is NaN
round, floor, ceiling
Rounds to closest, lowest, and highest, respec­tively
string.format
Num to String with optional formatting (format can be number, currency, percent, or computer)

Built In References - Date/t­ime­/da­te-time

date
Returns date only from a datetime value (Only use if variable is proble­matic)
time
Returns time only from a datetime value (Only use if variable is proble­matic)
datetime
Returns date and time from a datetime value (Only use if variable is proble­matic)
(date/­tim­e/d­ate­tim­e)_­if_­unknown
Marks a date-like value with specified missing subtype
iso_...
Converts date-like value to specified iso format. Depricated in favour of _format settings
string.format
Converts date-like object to string. See freemarker manual for formats
 

Common Flags

i
Case Insens­itive (eg. x==X)
f
First Only (Only apply to first occurance)
r
Substring is regex
m
Multi-line mode for regex
s
Enables dot-all mode for regex (. is wildcard)
c
Allows whitespace and comments in regex

Built In References - Hashes

Keys
A sequence with all the hash keys
Values
A sequence with all the hash values

Rarely Used Built-Ins

byte, double, float, int, long, short
Converts numbers to other types of numbers
eval
Evaluate a string as an FTL expression
has_co­ntent
Returns true if variable exists and isn't empty, false otherwise
interpret
Returns the value of a string interp­reted 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 = "­FQD­N"?n­ew(­opt­ions)
number­_to­_[d­ate­/ti­me/­dat­etime]
Converts a number to date/t­ime­/da­tetime, using the number of seconds since epoch

Template Language Quick Reference

assign name=value [in namesp­ace­hash]
Creates a variable called name
attemp­t/r­ecover
try/catch
compress
Removes all but one newline character between lines
escape x as x?[escape type]
<pr­e> 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. Takes optional parameters found on the docs page
functi­on/­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/els­eif­/else
Creates an if/els­eif­/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/­nes­ted­/return
Macro outputs a block of user-d­efined code which wraps nested (optio­nally, otherwise just outputs the code). return works as a break
noparse
<pr­e> 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. Don't use in normal situat­ions. Returns reason if set
switch­/ca­se/­def­aul­t/break
A regular switch block
t, lt, rt
Trim, left trim, right trim. Ignores whitespace based on tag position used
visit/­rec­urs­e/f­allback
Used for parsing trees, usually XML. See the ftl docs for full info

Download the Freemarker Directives Cheat Sheet

3 Pages
//media.cheatography.com/storage/thumb/suniahk_freemarker-directives.750.jpg

PDF (recommended)

Alternative Downloads

Share This Cheat Sheet!

 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Spring Framework 4 Cheat Sheet
          Selenium WebDriver Cheat Sheet Cheat Sheet