Sunday, October 18, 2009

FreeMarker: Functions & Macros

FreeMarker allows you to define Functions and Macros in ftl's as well.

Functions <#function>:

Syntax:

  • In FM you can define a function using a simple syntax as follows:
<#function functionName param1 param2>

  • Here "functionName" is the name of a function and param* shows the parameters that can be passed into the function
Calling Function:
  • To call a function in ftl's following syntax is used: (Consider the same function definition as define in the 'Syntax')
${functionName(param1Val, param2Val)}


<#return>:
  • The <#return> directive is not mandatory in functions. You can use <#return> any where in the function. Calling a <#return> exits from a function. If no such directive is used it will executes the whole function.

Macros <#macro>:

Syntax:
  • Syntax for <#macro> is almost same as <#function>. The name & parameters of macro are define in the same way as in the functions:
<#macro macroName param1 param2>


Calling Macro:
  • Calling a <#macro> is done by using "@" directive. Macro is itself a user-defined directive.
<@macroName param1=param1Val param2=param2Val/>

Recursion:
  • Macros can perform recursion (calling Macro inside Macro).
  • The recursion is same as done in normal functions.
  • Recursion Syntax:
<#macro macroName param1 param2>

<@macroName param1=param1Val param2=param2Val/>

< /#macro>


rizzz86

No comments: