Scripting With Freemarker

Herogi supports Freemarker for generating custom dynamic action parameters. You can create templates that has dynamic parameters to be changed according to event and session data while action is processed.

Freemarker is a powerful template language that you can use assignments, conditional blocks, string and arithmetic operations and formatting, macros and functions, and many more features. Here you can find some of them detailed;

Assign

You can define variables and assign values to them. Usage of assignment operator is like that;

<#assign name1=value1 name2=value2 … nameN=valueN>

For an email action, body parameter can be written like;

    <#assign username="${event.name.value()}"+" "+"${event.surname.value()}">

    <div>

        Hello ${username},
        <br><br>
        Welcome to <a href="http://herogi.com">Herogi</a>

    </div>

There is a variable defined as username and it is assigned by concatenating name and surname values of event. For an event that has a name parameter like Jenn and surname parameter like Brown, this template will be;


    <div>

        Hello Jenn Brown,
        <br><br>
        Welcome to <a href="http://herogi.com">Herogi</a>

    </div>

Conditions

You can use if, elseif and else to create different results according to different conditions.

Usage of if, elseif and else keywords are like that;

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

elseif and else are optional, you can use if without them.

Here is an example;


 <div>

      Hello <#if ${event.gender.value()} == "male">Mr.<#else>Mrs.</#if> ${event.surname.value()},
      <br><br>
      Welcome to <a href="http://herogi.com">Herogi</a>

 </div>

If the gender parameter is male and surname parameter is Brown then this template will be;


 <div>

      Hello Mr. Brown
      <br><br>
      Welcome to <a href="http://herogi.com">Herogi</a>

 </div>

Arithmetic Operations

You can use arithmetic operations to make calculations.

Here are some examples;

    <#assign averageAmount=(amount1+amount2)/2>
    <#assign profit=salePrice-cost>
    ${event.loginCount.value()+1}

Functions

You can use functions to simplfy your work.

Usage of a function is like that;

<#function name param1 param2paramN >
<#return returnValue >
</#function>

Here is an example of a function that concatenates name and surname and returns concatenated string;

    <#function concat name surname>
        <#return name+" "+surname>
    </#function>

If you call concat function like;

    ${concat("Jenn", "Brown")}

then it returns

    Jenn Brown

Loops

You can use list to iterate in a loop.

Usage of a list is like that;

<#list sequence as item ></#list>

Here is an example;

    <#list names as name>
        ${name}
    </#list>

Assuming names contains the [‘Jenn’, ‘Jack’, ‘Smith’] sequence, output of this template will be;

    Jenn
    Jack
    Smith

Parse string as json data

You can parse string as json data by using eval keyword and use parsed datas in your template.

Here is an example of parsing string as json data and usage of parsed datas;

    <#assign personDatas = person?eval>

    <#list person?keys as k>
      ${k} => ${personDatas[k]}
    </#list>

Assuming person as a json data like {name “:” Jenn “, “ surname “:” Brown “, “ age “: 25 }, output of this template will be;

    name => Jenn
    surname => Brown
    age => 25

Comment

You can add comments to your template to make it more readeble.

Here is an example;

    <#-- Enter your comment here -->

More about Freemarker

Freemarker has much more features than shown here, so you can visit Freemarker’s website by clicking here to discover more.