Template Syntax

Functions

You can define a function like this:

<function functionName(argName1, argName2...)>
    Some HTML...
</function>

A function can have zero or more arguments. The function name and argument names must be legal identifiers. You can call a function like this:

<call functionName(arg1, arg2...)>

You can also use functions as part of any expression. Note that for efficiency reasons it is better to use:

<call functionName(arg1, arg2...)>

than:

${functionName(arg1, arg2...)}

The arguments must be valid expressions. For example:

<function showLink(url, image, alt)>
    <a href="${url}">
    <if preferences.showImages>
        <img src="${image}" border="0" alt="${alt}">
    <else>
        ${alt}
    </if>
    </a>
</function>

...some HTML...

<call showLink(urls.home, images.home, "Home")>

...more HTML...

Functions can also return early by using the optional <exit> instruction. If an exit is not specified, the function will return at the closing </function> tag. For example:

<function showLink(url, image, alt)>
    <if url == "">
        <exit>
    </if>
    <a href="${url}">
    <if preferences.showImages>
        <img src="${image}" border="0" alt="${alt}">
    <else>
        ${alt}
    </if>
    </a>
</function>

...some HTML...

<call showLink(urls.home, images.home, "Home")>

...more HTML...

Functions are defined at compile time, so it's fine to call a function that's defined further down in the template. Make sure your function names don't conflict with your variable names. The template processor will get confused if they do, since variables and functions occupy the same namespace.

Note that functions, which are written in template language, are different from methods, which represent Java TemplateMethodModel or TemplateMethodModel2 objects in the data model. Functions are typically called by using the <call> tag. Methods are typically used the same way as the other data models.

Recursive function calls are supported.