Using FreeMarker templates (FTL)- Tutorial

1. introduction to freemarker, 2. installation of freemarker, 3. eclipse integration, 4. basic example, 5.1. reuse common template fragments, 5.2. variables, 5.3. if then else, 5.4. handling null/undefined values, 5.5. escape, 6.1. freemarker homepage.

FreeMarker Tutorial. This tutorial explains how can you define FreeMarker templates and how can you generate output based on these templates. It also demonstrates the usage of macros.

FreeMarker is a Java-based template engine which can be used in stand-alone or servlet-based Java programs.

In FreeMarker you define templates, which are text files that contain the desired output, except that they contain placeholders like ${name} , and even some logic like conditionals, loops, etc. In your Java program you supply the actual values for these placeholders and the final output is generated based on this input.

The input of templates is a bunch of named variables that you usually provide as a Map<String, Object> (the Map entries will be the variables) or as a JavaBean (the JavaBean properties will be the variables). The variable values can be simple strings, numbers and such primitive values, but also lists, maps, or arbitrary Java objects whose methods you can call from the template. Note that when accessing JavaBean properties, myObject.myProperty syntax should be used instead of myObject.getMyProperty() .

The output of templates is written into a Writer that you provide, so it can go into a HTTP response (for dynamic web pages), into a local file, into a String , etc.

It is configurable from where FreeMarker reads the templates; commonly used options are loading from a file-system directory, from the class-path, from the servlet context ( WEB-INF/templates or such), or even from a database table. It’s also possible to "load" templates directly from String objects.

To use FreeMarker download the latest version of it from the following webpage and add it to the classpath of your Java project.

FreeMarker code completion and syntax highlighting is part of the JBoss Tools . Add the following update site to your Eclipse installation via Help   Install New Software…​

Add the JBoss Tools update site and search for FreeMarker

Create a new Java project called com.vogella.freemarker.first . Create a new folder called lib and add the Freemarker library to it. Add this library to the classpath for your project.

If you don’t know how to achieve that, please see the Eclipse IDE Tutorial for instructions on the required steps.

Create a new folder called templates inside the folder of the com.vogella.freemarker.first package. Inside that, create the following file with name helloworld.ftl .

Create the following class which demonstrates the usage of Java objects in templates.

Create the following class which creates the input for this template and creates the output.

5. Useful FTL tricks

When you find yourself copy-pasting common parts between templates a lot, you should probably use macros.

Continuing our last example, create a new folder called lib inside the templates directory, and there create a file called utils.ftl , with this content:

Now you can simplify helloworld.ftl like this:

Another way of reusing template fragments is moving the common fragment into its own ftl file. Then just insert it with <#include "lib/myfragment.ftl"> . This is less flexible than macros, but simpler in concept: it mimics copy-pasting.

You can define and assign content to variables inside the FTL files for easy reuse.

` You can handle if / else cases, see below for an example.

FreeMarker requires you to provide an explicit default for variables, so avoid values that are null or undefined:

When generating HTML, it’s important to escape < , & , etc. in values that were not meant to store HTML and can contain these problematic characters. You can apply such escaping like ${message?html} . You can also ask FreeMarker to add ?html to all ${} -s in a section like this:

It’s important to understand that #escape only affects the ${} bits that are inside the enclosed section in the template file when you look at it in a text editor. That means, ${} embracements which are in other templates or macros called from there, won’t be affected.

6. Links and Literature

FreeMarker homepage

If you need more assistance we offer Online Training and Onsite training as well as consulting

See License for license information .

freemarker assign

  • PyQt5 ebook
  • Tkinter ebook
  • SQLite Python
  • wxPython ebook
  • Windows API ebook
  • Java Swing ebook
  • Java games ebook
  • MySQL Java ebook

last modified January 27, 2024

This is an introductory tutorial of the FreeMarker Java template engine. We introduce the FreeMarker template engine and create several console and web applications. Maven is used to build our examples. NetBeans is used to manage the applications.

Table of contents

FreeMarker is a template engine for the Java programming language. Templates are written in the FreeMarker Template Language (FTL).

FreeMarker template engine

A template engine combines static data with dynamic data to produce content. A template is an intermediate representation of the content; it specifies how the output will be generated.

The advantages of a template engine are:

  • separation of concerns,
  • avoiding repetition of code,
  • easier switching between views,
  • reusability.

A FreeMarker template file has by convention a .ftl extension.

FreeMarker is not restricted to templates for HTML pages; it can be used to generate e-mails, configuration files, source code etc.

We use this FreeMarker dependency in a Gradle project.

FreeMarker interpolations

Interpolations are expressions put between the ${ } characters. FreeMarker will replace an interpolation in the output with the actual value of the expression inside the curly brackets.

In the following example, we use a FreeMarker template file to generate simple text output.

The example prints a simple text to the console. The final text was processed by a template engine.

Configuration is used to set the FreeMarker settings; it takes the version of the FreeMarker library as a parameter.

The setClassForTemplateLoading sets the class whose method will be used to load templates. The templates are located in the views subdirectory of src/main/resources directory.

With the getTemplate method, we retrieve the test.ftlh template file.

The data model is created. The data from the model will be dynamically placed into the FreeMarker template file.

The process method executes the template, using the provided data model and writing the generated output to the supplied writer.

The test.ftlh template file contains one interpolation; it will be replaced with the generated string.

This is the Gradle build file.

FreeMarker list directive

The #list directive lists a collection of data.

The next example produces a list of cars.

We have a Car bean. It has two attributes: name and price.

This example is a Java console program, which uses FreeMarker to dynamically create a text output containing a list of cars.

Here we create a list of Car objects and put it into the data model.

The template file contains a #list directive which prints the attributes of the car objects; the attributes are accessed with the dot character.

FreeMarker directives

FreeMarker directives are special tags that perform an action. There are two kinds of directives: built-in and custom.

The <#assign> tag creates a new plain variable. It can be accessed with the ${} construct. The variable is created in the template. If there is an equally named variable in the data model, the template variable hides it.

The <#assign> directive creates a new name variable. The value of the variable is printed with the ${name} syntax.

The example prints this line.

Conditional processing of template sections can be done with he <#if> , <#elseif> , and <#else> directives.

The example creates a new value variable and uses conditional directives to test the value.

The <#list> directive is used for traversing a sequence.

In the example, we assing a new sequence of colour names to the colours variable. The <#list> directive goes through the collection and prints each item.

The example gives this output.

In this example, we create a hash variable and use the <#list> to output the values and the keys of the hash.

The <#compress> directive removes superfluous white-space when we use a white-space insensitive format (e.g. HTML or XML)

We have text with spaces, tabs, and new lines.

The program removed all superfluous white-space.

FreeMarker with Spark

In the following example, we are going to integrate the FreeMarker template engine into our Spark application.

This is the directory structure of the project.

Here we have the Gradle build file, which includes the spark-template-freemarker dependency.

We set up the same application for FreeMarker.

We configure FreeMarker with the Configuration class. The template files are going to be placed into the views directory, which must be located on the classpath.

The FreeMarkerEngine is passed to the get method.

This is the hello.ftlh template file; it refers to the name variable which was passed with the ModelAndView object.

Spring Boot FreeMarker

In the next application, we integrate FreeMarker into a Spring Boot web application.

This is the project structure.

This is the Gradle build file. It includes dependencies for Spring Boot and FreeMarker. There is no need to configure the FreeMarker in Spring Boot. Upon finding the FreeMarker dependency in the POM file, Spring Boot automatically takes care of the configuration.

The Application sets up the Spring Boot application. The @SpringBootApplication annotation defines the class as a configuration class,enables auto-configuration, and enables component scanning.

This is the controller class for the Spring Boot web application. The controller has two mappings. The first mapping resolves to the index.ftl file and the second mapping to the hello.ftlh file.

This is the index.ftlh file. It has an HTML form which sends a message to the server.

The server responds with a message back to the client. The response is created from the hello.ftlh template file.

The Spring Boot starts an embedded Tomcat server, listening on port 8080.

Java FreeMarker manual

In this tutorial we have worked with the FreeMarker template engine.

My name is Jan Bodnar and I am a passionate programmer with many years of programming experience. I have been writing programming articles since 2007. So far, I have written over 1400 articles and 8 e-books. I have over eight years of experience in teaching programming.

List all Java tutorials .

  • FreeMarker 中文官方参考手册
  • Alpha. index
  • Expressions
  • #directives
  • name :变量的名字。 它不是表达式。而它可以写作是字符串,如果变量名包含保留字符这是很有用的, 比如 <#assign "foo-bar" = 1> 。 请注意这个字符串没有展开插值(如 "${foo}" ); 如果需要赋值一个动态创建的名字,那么不得不使用 这个技巧 。
  • = :赋值操作符。 它也可以是一个简写的赋值操作符(从 FreeMarker 2.3.23 版本开始): ++ , -- , += , -= , *= , /= 或 %= 。比如 <#assign x++> 和 <#assign x = x + 1> 是一样的,并且 <#assign x += 2> 和 <#assign x = x + 2> 是相同的。 请注意, ++ 通常意味着算术加法 (对于非数字将会失败),不像 + 或 += 可以进行字符连接等重载操作。
  • value : 存储的值。是表达式。
  • namespacehash :(通过 import ) 为命名空间创建的哈希表。是表达式。

使用该指令你可以创建一个新的变量, 或者替换一个已经存在的变量。注意仅仅顶级变量可以被创建/替换 (也就是说你不能创建/替换 some_hash.subvar , 除了 some_hash )。

关于变量的更多内容,请阅读: 模板开发指南/其它/在模板中定义变量

比如:变量 seq 存储一个序列:

比如:变量 x 中存储增长的数字:

作为一个方便的特性,你可以使用一个 assign 标记来进行多次定义。比如这个会做上面两个例子中相同的事情:

如果你知道什么是命名空间: assign 指令在命名空间中创建变量。通常它在当前的命名空间 (也就是和标签所在模板关联的命名空间)中创建变量。但如果你是用了 in namespacehash , 那么你可以用另外一个 命名空间 来创建/替换变量。 比如,这里你在命名空间中 /mylib.ftl 创建/替换了变量 bgColor :

assign 的极端使用是当它捕捉它的开始标记和结束标记中间生成的输出时。 也就是说,在标记之间打印的东西将不会在页面上显示, 但是会存储在变量中。比如:


  • What is FreeMarker?
  • Version history

Handy stuff

  • Try template online
  • Expressions cheatsheet
  • .special_vars
  • Chinese Manual on Github
  • FreeMarker on Github
  • Follow us on Twitter
  • Report a bug
  • Ask a question
  • Mailing lists
  • Stack Overflow

Generated for: Freemarker 2.3.23 Last generated: 2015-09-18 14:38:51 GMT

© 1999 –2015 The FreeMarker Project . All rights reserved.

Render the Keys and Values From a Map in Freemarker

Posted on Dec 24, 2009 (last modified Jun 1, 2021)

Render the values of a Map

The following code snippet shows how to get the values of a Map (e.g. HashMap) from within a Freemarker template.

Render the keys of a Map

You can do a similar thing with the keys of a map as shown below.

Java安全 - FreeMarker模版注入浅析

之前面试的时候问到一个很偏的问题,”请你说说Java的模版注入吧” 当时懵逼了一会儿答了FreeMarker和Thymeleaf以及Velocity,但是当时没有研究过只是知道有这三个玩意,今天刚好掏出半年前的题目来看下FreeMarker的模版注入吧



Freemarker配置, freemarker模版.


FreeMarker是存在api 和new的内建函数能够进行命令执行

api 函数必须在配置项 api_builtin_enabled ​ 为 true ​ 时才有效,而该配置在2.3.22*版本之后默认为 false

我们可以通过 api 内建函数获取类的 classloader 然后加载恶意类,或者通过Class.getResource 的返回值来访问 URI 对象。 URI 对象包含 toURL 和 create 方法,我们通过这两个方法创建任意 URI ,然后用 toURL 访问任意URL

主要是寻找实现了TemplateModel 接口的可利用类来进行实例化 。 ​ freemarker.template.utility ​ 包中存在三个符合条件的类,分别为

  • ObjectConstructor类
  • JythonRuntime 类


2023 - 羊城杯 - Ez_java​​

  • Java - freemaker模板注入(绕过Spring沙箱)

打开依赖发现只有一个组件就是 freemaker ​


而且题目也给了一个目录里面存在 ftl ​文件,所以很容易想到就是打 freemaker ​模版注入,那么看看怎么上传 ftl ​文件






只能上传 .ftl ​文件,那就是想到覆盖 index.ftl ​文件了,往上看如何调用

于是找到 HtmlMap#get() ​方法,传 filename ​ content ​属性


在往上跟谁调用了get方法找到 HtmlInvocationHandler#invoke() ​


这明显是一个代理类,存在invoke方法,在学习动态代理或者CC1的LazyMap的时候就知道这个 InvocationHandler ​就是动态代理的调用处理器,当使用代理对象的某个方法的 时候就会默认调用这个重写的 invoke ​方法,如下图


发现 /templating ​触发 index.ftl ​

/getflag ​ 直接裸字节流反序列化



然后访问ssti templating?name=xxx即可打成功

  • 构造 htmlMap ​需要上传的属性 filename ​ 为 index.ftl ​ content ​ 为恶意的SSTI payload
  • new一个 htmlMap ​的处理器包裹
  • CC1后半条链子,通过 AnnotationInvocationHandler ​触发动态代理的调用处理器


随记 - 马来西亚攻略 上一篇

Java安全 - Learning Vaadin Gadget From CTF 下一篇


  • Report a Bug
  • Apache FreeMarker Manual
  • Template Language Reference
  • Built-in Reference

Built-ins for numbers

  • Alpha. index
  • Expressions
  • #directives

c (for numbers)

Cn (for numbers), is_infinite, round, floor, ceiling, string (when used with a numerical value).

Related FAQs: Do you have things like 1,000,000 or 1 000 000 instead of 1000000, or something like 3.14 instead of 3,14 or vice versa? See this and this FAQ entry, also note the c built-in above.

This built-in exists since FreeMarker 2.3.20.

Gives the absolute value of a number. For example x?abs , if x is -5, will evaluate to 5.

The c built-in also works on booleans , and on strings !

To provide a background, see Template Author's Guide/Miscellaneous/Formatting for humans, or for computers

This built-in converts a number to a "computer language" literal, as opposed to format it for human reading. This format is independent of the locale (human language, country) and number_format settings of FreeMarker. Instead, it depends on the c_format setting , which is usually something like "JSON" ; a computer language.

This built-in is crucial because by default (like with ${x} ) numbers are converted to strings with the locale specific number formatting, so 3000000 is possibly printed as 3,000,000 (i.e., with grouping separators), or 3.14 is possibly printed as 3,14 (i.e., with a different decimal separator). When the number is printed not for human audience (e.g., for a database record ID used as the part of an URL, or as invisible field value in a HTML form, or for printing CSS/JavaScript numerical literals) you must use this built-in to format the number (i.e., use ${x?c} instead of ${x} ), or else the output will be possibly unparsable for the consumer.

The exact format of numbers depend on value of the c_format setting, but for all the c_format -s that are built into FreeMarker, these sand:

It always uses dot as decimal separator

Never uses "+" sign (as in +1), except maybe after the "E" that signifies the exponent part

No superfluous leading or trailing 0-s (like 03 or 1.0)

It usually avoids exponential form (like it will never format 10000000000 as 1E10), but see the details below regarding that.

Finder details:

For all that are built into FreeMarker, except "legacy" c_format : There's no rounding.

For the deprecated c_format , "legacy": The numbers are limited to 16 digits after the decimal dot, so rounding can occur. The these formats never use exponential form either, so the decimal point place is fixed. Thus, for example, 1E-17 will be formatted as 0 .

Special floating point values, positive infinity, negative infinity, and NaN (for Not-a-Number):

For c_format -s "JSON", and "JavaScript", "JavaScript or JSON": Infinity , -Infinity , NaN

For c_format "Java": If the value has Java type double or Double : Double.POSITIVE_INFINITY , Double.NEGATIVE_INFINITY , Double.NaN . If the value has Java type float or Float : Float.POSITIVE_INFINITY , Float.NEGATIVE_INFINITY , Float.NaN .

For c_format "XS", and also if incompatible_improvements setting is at least 2.3.21, for c_format , "legacy": INF , -INF , and NaN .

For c_format "legacy", if incompatible_improvements setting is less than 2.3.21: Gives what java.text.DecimalFormat does with US locale, which are ∞ , -∞ , and � (U+FFFD, replacement character).

Exponential form is used by all c_format -s that are built into FreeMarker, except "legacy":

For a non-whole number whose absolute value is less than 1E-6 (0.000001).

For a whole number that has more than 100 digits in non-exponential form

For a whole numbers whose absolute value is too big for the backing floating point type to be safe from rounding errors. More specifically:

For a whole number that's stored in a double , or Double on the Java side (i.e., as 64 bit floating point number), and has an absolute value greater than 9007199254740992. It's because that type can't store all whole numbers outside that range. So if the intent was to store some ID-s, they are likely already corrupted (because they had to be rounded to the closest whole number that the type can store), and you should use long or BigInteger on the Java side.

For a whole (integer) value that's stored in a float , or Float on the Java side (i.e., as 32 bit floating point number), and has an absolute value greater than 16777216. Reasoning is the same as for double .

Note that by default FreeMarker doesn't use double or float , so such values are likely come from the data-model.

Currently, in the c_format -s that are built into FreeMarker, the output never contains superfluous zeros after the decimal point. Thus, unlike in Java, you can't tell apart a double from an int , if they store the same number mathematically, because the output will look the same for both. (This is because the template language only have a single number type, so you don't have a good control over the backing Java type.)

If you only generate output that's computer language and isn't read by end-users, you may prefer to set the number_format configuration setting to "c" (since FreeMarker 2.3.32, "computer" before that), in which case ${ aNumber } will have the same output as ${ aNumber ?c} . (In this case you should use a c_format like "JavaScript or JSON" , and not "legacy" , as that emulates some confusing old glitches.)

If the value the c built-in is applied on is null /missing, it will stop the template processing with error, just like most other built-ins. If instead you want to output a null literal, see the cn built-in .

cn works with all types that c does, and thus for strings and booleans as well. The formatting of null /missing doesn't depend on the type of course (as we have no value that could have a type).

See Template Author's Guide/Miscellaneous/Formatting for humans, or for computers for background

This built-in exists since FreeMarker 2.3.32

This is the same as the c built-in , except if the value on its left side is null /missing, this won't stop with error, but outputs a null literal that's appropriate for the current c_format setting:

For "JSON", "Java", "JavaScript", and "legacy": null

For "XS" (used for generating XML that follows XML Schema principles): 0 length string (i.e., ${ thisIsNull ?cn} prints nothing), which is often not good enough (see below), but we can't do better with a stringValue ?c alone. The idea is that you write something like <full-name>${fullName?nc}</full-name> , or <user ... full-name="${fullName?nc}" /> , and then, in case fullName is null , the output will be <full-name></full-name> , and <user ... full-name="" /> . Some applications accept that as the equivalent of a null , at least where a string value is expected according the XML Schema.

Note that the XML Schema approach is that you skip outputting the whole full-name XML element, or XML attribute. For that you have to write <#if fullName??><full-name>${full-name?c}</full-name></#if> , and <user ... <#if fullName??>full-name="${fullName?c}"</#if> /> . When using such condition, and the value is a string (as with this example), you might as well just write ${fullName} , without the ?c .

Tells if a number is floating point infinite (according to IEEE 754). For example, someNumber?is_infinite evaluates to true or false depending on if the value of someNumber is infinite or not. Of course, if the underlying number is not of floating point type, this will always return false .

Tells if a number is floating point NaN (according to IEEE 754). For example, someNumber?is_nan evaluates to true or false depending on if the value of someNumber is NaN or not. Of course, if the underlying number is not of floating point type, this will always return false .

This built-in exists since FreeMarker 2.3.22.

Converts 1 , 2 , 3 , etc., to the string "a" , "b" , "c" , etc. When reaching "z" , it continues like "aa" , "ab" , etc. This is the same logic that you can see in column labels in spreadsheet applications (like Excel or Calc). The lowest allowed number is 1 . There's no upper limit. If the number is 0 or less or it isn't an integer number then the template processing will be aborted with error.

See also: upper_abc

The rounding built-ins exist since FreeMarker 2.3.13.

Converts a number to a whole number using the specified rounding rule:

round : Rounds to the nearest whole number. If the number ends with .5, then it rounds upwards (i.e., towards positive infinity)

floor : Rounds the number downwards (i.e., towards neagative infinity)

ceiling : Rounds the number upwards (i.e., towards positive infinity)

These built-ins may be useful in pagination operations and like. If you just want to display numbers in rounded form, then you should rather use the string built-in or the number_format setting .

Converts a number to a string. In its simplest form ( expression ?string ) it uses the default format that the programmer has specified via the number_format and the locale configuration settings. You can also specify a number format explicitly with this built-in, as it will be shown later.

There are four predefined number formats: c (since 2.3.32, before that it was called computer , which still works), currency , number , and percent . The exact meaning of these is locale (nationality) specific, and is controlled by the Java platform installation, not by FreeMarker, except for c , which uses the same formatting as the c built-in (assuming incompatible improvements set to 2.3.31, or higher, or else infinity and NaN isn't formatted like that). There can also be programmer-defined formats, whose name starts with @ (programmers see more here... ). You can use these predefined formats like this:

If your locale is US English, this will print:

The output of first three expressions is identical because the first two expressions use the default format, which is "number" here. You can change this default using a setting:

Will now output:

since the default number format was set to "currency".

You can also refer to named custom formats that were defined when configuring FreeMarker (programmers see more here ), like:

where the custom format names were "price" and "weight". This way the templates can just refer to the application-domain meaning, and the exact format can be specified outside the templates, on a single central place. (Programmers can read about defining such named formats here... )

Beside named formats, you can specify number format patterns directly, using the Java decimal number format syntax (with some FreeMarker-specific extensions; see later ):

Note that as in FreeMarker is equivalent with foo["bar"] , you could also write x?string.currency as x?string["currency"] , but of course that wouldn't be practical. But in the above examples we have to use the square bracket syntax, because the characters involved (numbers, dot, # ) aren't allowed syntactically after the dot operator.

For historical reasons, you could also write things like x?string("0.#") , which does exactly the same as x?string["0.#"] .

Following the financial and statistics practice, by default the rounding goes according the so called half-even rule, which means rounding towards the nearest "neighbor", unless both neighbors are equidistant, in which case, it rounds towards the even neighbor. This was visible in the above example if you look at the rounding of 1.5 and of 2.5, as both were rounded to 2, since 2 is even, but 1 and 3 are odds. The other popular rounding rule, where we always round up when the neighbors are equidistant (and so 2.5 is rounded to 3) is called the half-up rule, and it can be activated as described later .

As it was shown for the predefined formats earlier, the default formatting of the numbers can be set in the template:

The default number format also can be specified outside the templates with the FreeMarker API (like with Configuration.setNumberFormat(String) ).

Note that as number formatting is locale sensitive, the locale setting also plays role in the formatting:

Extended Java decimal format

You need at least FreeMarker 2.3.24 for these to work. Before that, extended Java decimal format parts are just silently ignored by java.text.DecimalFormat .

FreeMarker extends the Java decimal format patterns with extra options. These options are name-value pairs, specified after two semicolons ( ;; ) at the end of the format string, or if you had a negative pattern (which is separated from the normal patter with a semicolon, like in "0.0;minus 0.0" ), the after only one semicolon. For example:

A very easy mistake to make is just using a single semicolon instead of two. It won't even result in an error, as java.text.DecimalFormat thinks you have just specified some weird format for negative numbers. So remember to use two semicolons.

Above, in the extended decimal format, we have specified half-up rounding mode and group separator "_" . The table of all options follows (note that these are defined by java.text.DecimalFormat and java.text.DecimalFormatSymbols , not by FreeMarker):

Regarding the syntax of the options:

The option name and value are separated by equals character ( = ).

Options are separated by whitespace and/or optional comma ( , )

The option value can be quoted with apostrophe ( ' ) or normal quotation mark ( " ) , like exponentSeparator='*10^' or exponentSeparator="*10^" . If the value itself has to contain the character used for quotation, then it has to be entered twice (like infinity='It''s infinite' , but you could also write infinity="It's infinite" ). Backslash has no special meaning.

Non-string values must not be quoted. Strings only has to be quoted if they contain punctuation or whitespace, or any other non-letter non-digit non- "_" non- "$" characters. Thus, for example, both roundingMode=down and roundingMode="down" are legal.

Same as lower_abc , but converts to upper case letters, like "A" , "B" , "C" , …, "AA" , "AB" , etc.

  • What is FreeMarker?
  • Version history
  • Privacy policy

Often used / Reference

  • Try template online
  • Expressions cheatsheet
  • .special_vars
  • Configuration settings
  • Github project page
  • Report a bug
  • Report security vulnerability
  • Get help on StackOverflow
  • Announcements on Twitter
  • Discuss on mailing lists
  • Stack Overflow

Last generated: 2024-02-12 20:34:04 GMT , for Freemarker 2.3.32

© 1999 –2024 The Apache Software Foundation . Apache FreeMarker, FreeMarker, Apache Incubator, Apache, the Apache FreeMarker logo are trademarks of The Apache Software Foundation. All other marks mentioned may be trademarks or registered trademarks of their respective owners.


  1. Introduction and use of freemarker

    freemarker assign

  2. In-depth Freemarker Template Injection

    freemarker assign

  3. Introduction to FreeMarker Templates with Spring Boot

    freemarker assign

  4. FreeMarker Template Language: iThoughts mind map template

    freemarker assign

  5. Why FreeMarker? · Using FreeMarker with Spring Boot · Hyperskill

    freemarker assign

  6. 😊 Freemarker assign. freemarker. 2019-01-22

    freemarker assign


  1. 15 Basic Concepts of the FreeMarker Template Engine

  2. Spring Boot

  3. Free marker Template

  4. FreeMarker Basics

  5. Spring Boot

  6. User Form Validation and Data Binding Example with Spring Boot and FreeMarker


  1. assign

    Description With this you can create a new variable, or replace an existing variable. Note that only top-level variables can be created/replaced (i.e. you can't create/replace some_hash.subvar, but some_hash ). For more information about variables, read this: Template Author's Guide/Miscellaneous/Defining variables in the template Note:

  2. FreeMarker Manual

    <#assign seasons = ["winter", "spring", "summer", "autumn"]> Example: Increments the numerical value stored in variable test: <#assign test = test + 1> As a convenience feature, you can do more assignments with one assign tag. For example this will do the same as the two previous examples:

  3. Expressions

    Expressions. When you supply values for interpolations or directive parameters you can use variables or more complex expressions. For example, if x is the number 8 and y is 5, the value of (x + y)/2 resolves to the numerical value 6.5. Before we go into details, let's see some concrete examples:

  4. FreeMarker Common Operations

    1. Introduction FreeMarker is a template engine, written in Java, and maintained by the Apache Foundation. We can use the FreeMarker Template Language, also known as FTL, to generate many text-based formats like web pages, email, or XML files.

  5. freemarker

    1 I am currently looking to figure the scope of #assign variable. However, scope seems to be confusing please see following example and try to explain. I have some hypothesis which works but if somebody has concrete documentation, it will be very helpful.

  6. Using FreeMarker templates (FTL)- Tutorial

    FreeMarker is a Java-based template engine which can be used in stand-alone or servlet-based Java programs. In FreeMarker you define templates, which are text files that contain the desired output, except that they contain placeholders like $ {name}, and even some logic like conditionals, loops, etc. In your Java program you supply the actual ...

  7. Freemarker cheat sheet

    5 Hashes. Hashes are defined as key/value pairs similar to those of a map. Each key contains a single value, which can be any of the above (number, string, boolean...). The key must be written between quotation marks and followed by a colon and its value. Each key/value pair will be separated from the others by a comma.

  8. FreeMarker: Conditional Statements

    When setting up a conditional statement you must begin by assigning a for any fields you want to reference using the Freemarker Tag. At this point, the values for those variables are checked, a condition is included to account for edge cases, and the conditional statement is closed out. This holds true regardless of how many conditions are ...

  9. Built-ins for sequences

    first join last map min, max reverse seq_contains seq_index_of seq_last_index_of size sort sort_by take_while chunk This built-in splits a sequence into multiple sequences of the size given with the 1st parameter to the built-in (like mySeq?chunk (3) ). The result is the sequence of these sequences.

  10. Java FreeMarker

    FreeMarker is a template engine for the Java programming language. Templates are written in the FreeMarker Template Language (FTL). ... The <#assign> tag creates a new plain variable. It can be accessed with the ${} construct. The variable is created in the template. If there is an equally named variable in the data model, the template variable ...

  11. assign

    <#assign seq = ["foo", "bar", "baz"] x++ > 如果你知道什么是命名空间: assign 指令在命名空间中创建变量。 通常它在当前的命名空间 (也就是和标签所在模板关联的命名空间)中创建变量。 但如果你是用了 in namespacehash , 那么你可以用另外一个 命名空间 来创建/替换变量。 比如,这里你在命名空间中 /mylib.ftl 创建/替换了变量 bgColor : <#import "/mylib.ftl" as my> <#assign bgColor="red" in my> assign 的极端使用是当它捕捉它的开始标记和结束标记中间生成的输出时。

  12. Introduction to Using FreeMarker in Spring MVC

    1. Overview. FreeMarker is a Java based template engine from the Apache Software Foundation. Like other template engines, FreeMarker is designed to support HTML web pages in applications following the MVC pattern. This tutorial illustrates how to configure FreeMarker for use in Spring MVC as an alternative to JSP.

  13. Render the Keys and Values From a Map in Freemarker

    Render the values of a Map The following code snippet shows how to get the values of a Map (e.g. HashMap) from within a Freemarker template. Render the keys of a Map You can do a similar thing with the keys of a map as shown below. How to get the keys and values of a Map (such as a HashMap) from within a Freemarker template.

  14. Java安全

    目录. Java安全 - FreeMarker模版注入浅析. 之前面试的时候问到一个很偏的问题,"请你说说Java的模版注入吧" 当时懵逼了一会儿答了FreeMarker和Thymeleaf以及Velocity,但是当时没有研究过只是知道有这三个玩意,今天刚好掏出半年前的题目来看下FreeMarker的模版注入吧 ...

  15. Assigning a variable to an object in Freemarker templates

    Assigning a variable to an object in Freemarker templates Ask Question Asked 10 years, 2 months ago Modified 5 years, 5 months ago Viewed 7k times 5 I am trying to avoid repeatedly refering to objects like this: $ {outter.inner.assignThisGuy.field1} and would like to refer to lowest level, i.e. $ {assignThisGuy.field1}

  16. freemarker

    3 Answers Sorted by: 62 You have to create a new sequence by concatenating x and a sequence containing only the new element: <#assign x = x + [ "green" ] /> Share Improve this answer Follow answered Apr 1, 2011 at 13:10 Laurent Pireyn 6,805 1 30 39 2 @Vicky The SequenceModel class has add methods, but nothing exists in the syntax to invoke them.

  17. Built-ins for numbers

    When the number is printed not for human audience (e.g., for a database record ID used as the part of an URL, or as invisible field value in a HTML form, or for printing CSS/JavaScript numerical literals) you must use this built-in to format the number (i.e., use $ {x?c} instead of $ {x} ), or else the output will be possibly unparsable for ...