FreeMarker Templates

iKnowBase supports the use of FreeMarker templates. When using FreeMarker templates, you will combine static markup, standard FreeMarker functionality and iKnowBase model objects to create the desired output:

In order to best utilize the FreeMarker technology, you should read the FreeMarker documentation available at http://freemarker.org/docs. Some key features are described below, but this covers only a small subset of the capabilities.

About FreeMarker

With FreeMarker you can create HTML-based templates that can display data from a Java model. The following figure shows how FreeMarker connects with a template (in this instance an HTML template), and data from a Java object model. This connection generates an HTML output that displays the data in the template.

Includes and imports

FreeMarker supports <#include> and <#import> as mechanisms to reuse templates. With iKnowBase, it is possible to use both of these mechanisms to refer to templates stored inside the iKnowBase repository:

FreeMarker Directives

FTL tags are used to call directives. There are two types FTL tags:

These tags are similar to the syntaxes in the HTML code and the XML code except for tags that start with #. There are two types of directives: predefined directives and user-defined directives. User defined directives use @ instead of #. For example, <@mydirective parameter>... </@mydirective>.

The format of the parameters depends on the name of the directive.

Property Description
if, else, elseif

Syntax:

    <#if condition>
    ...
    <#elseif condition2>
    ...
    <#elseif condition3>
    ...
    <#else>
    ...
    </#if>

For example:

    <#if x == 1>
        x is 1  
    <#elseif x == 2>
        x is 2  
    <#else>
        x is neither 1 nor 2
    </#if>

ikbProperty. switch, case, default, break

Syntax:

    <#switch value>
        <#case refValue1>  
        ...
        <#break>
        <#case refValue2>
        ...
        <#break>
        ...
        <#case refValueN>
        ...
        <#break>
        <#default>
        ...
    </#switch>

For example:

    <#switch x>
        <#case x = 1>
        x is 1
        <#case x = 2>
        x is 2
        <#default>
        x is neither 1 nor 2
    </#switch>

ikbProperty. list, break

This directive is used to loop through a list of items:

    <#list sequence as item>
        Item = ${item}
    </#list>

p.For example:

    <#assign seq = ["winter", "spring", "summer", "autumn"]>
    <#list seq as x>
        ${x_index + 1}. ${x}<#if x_has_next>,</#if>  
    </#list>

ikbProperty. Assign

This directive is used to create a variable:

<#assign name=value>

or

    <#assign name>
    This is the value assigned to name
    </#assign>

For example:

<#assign seasons = ["winter", "spring", "summer", "autumn"]>

If you want to write values directly within the insertion brackets ${ and }, there are some characters that need a specific syntax.

Syntax Description
\" Quotation mark (u0022)
\' Apostrophe (apostrophe-quote) (u0027)
\\ Back slash (u005C)
\n Line feed (u000A)
\r Carriage return (u000D)
\t Horizontal tabulation (tab) (u0009)
\b Backspace (u0008)
\f Form feed (u000C)
\l Less than sign: <
\g Greater than sign: >
\a Ampersand: &
\xCode Character given with its hexadecimal Unicode (UCS) code

Built-in Functions

FreeMarker contains a set of built-in functions that you can use directly with variables.

Built-ins for strings

Function Syntax and Description
html The ${string?html} variable is coded to the HTML text. For example, the character < becomes &lt.
js_string The ${string?js_string} variable is coded to the JavaScript text. For example, the character ‘ becomes ’'.
xml The ${string?xml} variable is coded to the XML text. For example, the character > becomes &gt.
url The ${string?url} variable is coded to the URL text. For example, the character / becomes %2b.
cap_first The ${string?cap_first} variable has the initial character capitalized.
lower_case The ${string?lower_case} variable gets all the characters decapitalized.
upper_case The ${string?upper_case} variable gets all the characters capitalized.
trim The ${string?trim} variable peels off all blank characters at the start and end.
substring This function uses ${string?substring(from, [to])} tag to take a segment of the variable depending on specified positions.
replace The ${string?replace(“this”, “with that”)} variable substitutes the text with other text.
number This function uses ${string?number} tag to convert a variable from string to number.

Built-ins for numbers

Function Syntax and Description
string This function uses ${number?string} tag to convert a variable from number to text.
string.number This function uses ${number?string.number} tag to convert a variable from number to text.
string.percent This function uses ${number?string.percent} tag to convert a variable from number to percent text.

Built-ins for dates

Function Syntax and Description
string This function uses ${date?string} tag to convert a variable from date to text.
string.short This function uses ${date?string.short} tag to convert a variable from date to a predefined short text.
string.medium This function uses ${date?string.medium} tag to convert a variable from date to a predefined medium text.
string.long This function uses ${date?string.long} tag to convert a variable from date to a predefined long text.
string.full This function uses ${date?string.full} tag to convert a variable from date to a predefined full text.