Alternative (square bracket) syntax - Apache FreeMarker Manual
Unsupported web browser - Use a modern browser to view this website!
Manual
Java API
Contribute
Report a Bug
Manual
Apache FreeMarker Manual
Template Author's Guide
Miscellaneous
Alternative (square bracket) syntax
Bookmarks:
Alpha. index
Glossary
Expressions
?builtins
#directives
.spec_vars
FAQ
Previous
Next
Alternative (square bracket) syntax
Page Contents
Square bracket tag syntax
Square bracket interpolation syntax
Sometimes the generated content uses symbols that clashes with
the default FreeMarker syntax (typically,
${
...
-s that FreeMarker
should print as is, without interpretation), or you use some tool that
is confused by the default FreeMarker syntax (typically by
and
). While usually
there are tricks to work those cases around (like you can use
${'$'}{x}
to print
${x}
as is) ,
they are often too inconvenient. Thus, the interpolation syntax can be
configured to be like
[=x]
instead of
${x}
. Also, independently of that, the FreeMarker
tag syntax can be configured to use
[]
, like in
[#if x]
...
[/#if]
Note:
While both the "tag syntax" and
"interpolation syntax" can be configured to use square
brackets, they are totally independent configuration settings. Thus,
the overall syntax can be like
[#if x]${y}[/#if]
or like
<#if x>[=y]#if>
as
well.
Square bracket tag syntax
Note:
This section is about the
tag
syntax,
not the
interpolation
syntax
. Don't confuse the two, they are independent.
Note:
This feature exists since FreeMarker 2.3.4.
FreeMarker supports an alternative tag syntax, where
and
is used instead of
and
in FreeMarker
directives and comments, for example:
Calling predefined directive:
[#list animals as
animal]
...
[/#list]
Calling user-defined directive:
[@myMacro
/]
Comment:
[#-- the comment --]
To use square tag syntax instead of the default one, the
programmers should configure FreeMarker so
(see
Configuraton.setTagSyntax
, or the
tag_syntax
setting)
. However, the tag
syntax can also be enforced in the template, with the
ftl
directive
(see later).
For example, this is how the last example of the
Getting Started
looks with
the alternative syntax:
Template
We have these animals:
| Name | Price [#list animals as animal] |
|---|---|
[#if animal.size == "large"] [/#if] ${animal.name} [#if animal.size == "large"] [/#if] | ${animal.price} Euros [/#list] |
The square bracket and the default (angle bracket) syntax are
mutually exclusive within a template; they can't be mixed. If the
template uses square bracket tag syntax, then things like
<#if
...
will
be just static text, not FTL tags. Similarly, if the template uses
the angle brackets tag syntax, things like
[#if
...
are static text, not FTL
tags.
If you start the file with
[#ftl
...
(where the
...
stands for the
optional parameters; of course
[#ftl]
works too)
the file will use square bracket
tag
syntax
regardless of the configuration settings (but that does
not
change the interpolation syntax to
[=...]
). Similarly, if you start the file with
<#ftl
...
the
file will use the normal (angle bracket) tag syntax. If there is no
ftl
directive in the file, then the programmer
decides what the tag
syntax
will be by
configuring FreeMarker
(programmers
see
Configuration.setTagSyntax(int)
in the API
javadocs)
. Most probably the programmers use the factory
default.
Square bracket interpolation syntax
Note:
This section is about the
interpolation
syntax, not the
tag
syntax
. Don't confuse the two, they are independent.
Note:
This feature exists since FreeMarker 2.3.28
In this case instead of
${
expression
(and the
deprecated
#{
expression
) you
write
[=
expression
This syntax is activated by the programmers from the configuration
(see
Configuration.setInterpolationSyntax
in the Java
API)
; unlike the tag syntax, it can't be specified inside
the template. It can be used both together with, and without the
square bracket
tag
syntax
, as they are technically
unrelated, but it's probably more aesthetic to use square bracket
tag syntax when you use square bracket interpolation syntax:
Template
[#--
Note:
This example uses both interpolation_syntax=squareBracket and tag_syntax=squareBracket,
but you can also use interpolation_syntax=squareBracket and tag_syntax=angleBracket.
--]
We have these animals:
| Name | Price [#list animals as animal] |
|---|---|
[#if animal.size == "large"][/#if] [=animal.name] [#if animal.size == "large"][/#if] | [=animal.price] Euros [/#list] |
When square bracket interpolation syntax is used,
${
expression
and
#{
expression
in the
template will be just static text, which is printed as is. This is
mostly useful if you generate output that should contain those
(especially
${
expression
is
frequent), such as when generating JSP files.
There's also a third tag syntax, "dollar", where
only the interpolation syntax is
${
expression
, and the
deprecated
#{
expression
is just
static text. (The one where
#{
expression
is still
an interpolation is called the "legacy" interpolation
syntax, and is the default for backward compatibility.)
Previous
Next
Overview
What is FreeMarker?
Version history
FAQ
License
Often used / Reference
Try template online
Expressions cheatsheet
#directives
?built_ins
.special_vars
Configuration settings
Community
Github project page
Report a bug
Report security vulnerability
Get help on StackOverflow
Announcements on Twitter
Discuss on mailing lists
Github
Stack Overflow
Edited with XMLMind XML Editor
Last generated:
2025-06-22 15:04:39 GMT
, for Freemarker 2.3.34
1999
–2025
The Apache Software Foundation
. Apache FreeMarker, FreeMarker, Apache, the Apache feather logo, and the Apache FreeMarker project logo are trademarks or registered trademarks of The Apache Software Foundation. All other marks mentioned may be trademarks or registered trademarks of their respective owners.