Freemarker Quick Reference
Reports need to access attributes of an object. The most common types of attributes are discussed below:
String
Eg., "TemplateTitle", "20220322 13:56:44"
To access an attribute of type String, strAttribute of an object, use the syntax: ${object.strAttribute}
${template.templateTitle}
displays Battery Installation for the Battery Installation Template.
Number(Integer/Double)
Eg., Integer - 4, 6, 890; Double - 546.34 , 56.98, 0.25
An Integer is a whole number type that does not contain decimal points.
A Double number type contains a decimal point.
Note
The separator used to demarcate the whole number part and the decimal fraction part of a Double is a period(.) and not a comma.
To access an attribute of type Number (Integer/Double), numAttribute of an object, use the syntax: ${object.numAttribute}
${template.componentType}
displays 1 if the Template can be run as a Component and 0 if it cannot be run as a Component.
${job.timeTracked}
displays 1.32457
Boolean
Eg., true, false
To access an attribute of type Boolean, boolAttribute of an object, use the syntax: ${object.boolAttribute}
${template.attachedReports}
displays True if reports are attached to the Template.
Note
The Boolean value can be just displayed or used in conditions as well.
The following example illustrates a scenario where a Boolean value is used in a condition.
<#if template.attachedReports >
//do this
<#else>
//do this
</#if>
Hashmap
Eg., {"name" : "mouse", "price" : 50}
A Hashmap stores a key and value pair. Each value is associated with a unique key value. The below hashmap stores the details of a device.
Key | Value |
---|---|
name | mouse |
price | 50 |
You can access the value of a map directly by its key using the following syntax:
${key} = ${object[key]}
The above hashmap is defined as follows:
<#assign deviceHashmap = {"name":"mouse", "price":"50}>
You can access the value directly by providing the key. In the following example, the value 50 is assigned to the variable cost.
${cost} = ${deviceHashmap["price"]}
You can directly print the value by referencing the key in the hashmap as shown below.
${deviceHashmap["name"]}
displays the corresponding value "mouse".
${deviceHashmap["price"]}
displays the corresponding value 50.
${job.stringVar[Assigner]}
displays AssignerName of the Job.
List
A collection of items. Eg., 1,2,4,5,6 or "String1","String2","String3"
To access an attribute listAttribute of type list of an object, assign the list attribute to a variable.
<#list object.listAttribute as item>
Set of instructions repeated for each item
</#list>
where item
is the name of the loop variable (not an expression)
Eg., template.allDocumentationPublicKeys
displays 345654, 342234
Note
Collections, ArrayLists etc. are the same as Lists.
There could be multi-dimensional lists like List<List<String>> or List<List<List<Map<String, Object>>>> etc.
FieldLogs Objects
Eg., ${template.effectivity}
, ${job.task}
Important
The list of Objects can be found in the matrix here.
To access an attribute objectAttribute which in itself an object, reference it from the root object as shown.
${rootObject.objectAttribute.attribute}
${template.effectivity.effectivityId}
displays the EffectivityID of the Template.
Freemarker Directives
The Freemarker file(.ftl
file) has a structure similar to a .html
file. It contains three sections:
<head>
: Contains the css of the output.<#macro>
: Contains the definition of the macros used in the body.<body>
: Contains all the content of your report (header, body and footer), queries and macros.
You can view the complete list of Freemarker directives here. Some commonly used Freemarker directives are listed below:
assign
The assign directive creates a new variable or replace an existing variable.
Syntax:
<#assign name1 as value1>
</#assign>
where name1
is the name of the variable and value1
is the value to assign to the variable.
Example:
<#assign varEffectivity as job.effectivityText>
</#assign>
if, else, elseif
You can use if, elseif and else directives to conditionally skip a section of the template.
Syntax:
<if condition>
...
<#elseif condition2>
...
<#else>
...
</#if>
where condition, condition2 must evaluate to a boolean value.
Example:
<#if executedStep.columnsCount == 1>
ColumnsCount is 1
<#elseif executedStep.columnsCount == 2>
ColumnsCount is 2
<#else>
ColumnsCount is greater than 2
</#if>
include
The include directive inserts another FreeMarker template file (specified by the path parameter) into your template. The output from the included template is inserted at the point where the include tag occurs. The included file shares the variables with the including template, similarly like if it was copy-pasted into it.
Syntax:
<#include path>
Example:
<-- Code to print the report -->
<#include "/common/disclaimer.ftl">
list
The list directive executes the code in he body of list for each value in the sequence (or collection) specified as its first parameter. For each such iteration the loop variable will store the value of the current item.
Syntax:
<#list sequence as item>
Part repeated for each item
</#list>
Example:
<#list template.allDocumentationPublicKeys as allDPKeys>
${allDPKeys}
</#list>
macro
A macro is a template fragment associated with a variable. You can use that variable in your template as a user-defined directive, so it helps in repetitive tasks.
A macro variable is created first. It can then be placed anywhere in the template.
Syntax:
<#macro name>
...
</#macro>
Example:
<#macro test>
...
</#macro>
<#-- call the macro; the macro variable is already created: -->
<@test/>
switch, case, default, break
Switch is used to choose a fragment of template depending on the value of an expression.
Syntax:
<#switch value>
<#case refValue1>
...
<#break>
<#case refValue2>
...
<#break>
<#default>
...
</#switch>
where value
, refValue1
, refValue2
are expressions that evaluate to scalars of the same type.
Example:
<#switch $template.package>
<#case "Package1">
Template is in Package1
<#break>
<#case "Package2">
Template is in Package2
<#break>
<#default>
Template is not in Package1 or Package2
</#switch>
Comments
Comments enhance readability and add clarity to the code.
Syntax:
<#-- comment -->
Example:
<#-- Run this macro if Template can be run as a Component -->