Defining variables in the template

[TODO: This section is under rewriting and is incomplete!]

As we have described, a template can use the variables defined in the data-model. A template can also define variables outside the data-model for its own use. These temporary variables can be created and replaced using FTL directives. Note that each template processing job has its own private set of these variables that exists while the given page is being rendered. This variable set is initially empty, and will be thrown away when the template processing job has been finished.

You access a variable that you have defined in the template exactly as if it were a variable in the data-model root. The variable has precedence over any variable of the same name defined in the data model. That is, if you define a variable called ''foo'' and coincidentally, there is a ''foo'' in the data-model as well, then the variable created in the template will hide (not overwrite!) the variable in the data-model root. For example, ${foo} will print the value of the variable created in the template.

For information about syntax of variable references please read: The Template/Expressions

Quick introduction for 2.4 preview testers

We have two new directives, set and var, which deprecate assing/local/global. At the first glance the set directive is the same as assign; [#set x = 1] has the same effect as [#assign x = 1]. But, with the var directive you can declare a variable in the lexical scope of the block where it occurs, and set will set the variable in the innermost scope that declared the variable (while assign always created/modified the variable on the top-level, i.e., in the "main namespace"). This for example can be utilized to create local variables in macros (hence the local directive is now deprecated):

[#macro myMacro]
  [#var x] [#-- x is a local variable --]
  [#set x = "Set in myMacro"]
  [#set y = "Set in myMacro too"] [#-- y is not a local variable --]
  x: ${x}
  y: ${y}
[/#macro]

[#set x = "On the top level"]
[#set y = "On the top level too"]
x: ${x}
y: ${y}
[@myMacro /]
x: ${x}
y: ${y}  

which will print:

x: On the top level
y: On the top level too
  x: Set in myMacro
  y: Set in myMacro too
x: On the top level
y: Set in myMacro too  

Note that the var directive can also specify the initial value, so earlier we could write just:

[#macro myMacro]
  [#var x = "In myMacro"]
  ${x}
[/#macro]
...  

The var directive can declare variables in arbitrary blocks, not only in macros. This was no possible at all in 2.3. Example:

[#set x = "Top level"]
${x}
[#if true]
  [#var x = "Inner 1"]
  ${x}
  [#if true]
    [#var x = "Inner 2"]
    ${x}
  [/#if]
  ${x}
[/#if]
${x}  

This prints:

Top level
  Inner 1
    Inner 2
  Inner 1
Top level  

Of course instead of if directive we could use list as well, or even user-defined directives. It works with all. But of course it doesn't consider HTML tags, since that's just static text for FreeMarker, not tags.

Now a few example to explain some marginal cases and answer common questions and misunderstandings:

FreeMarker Manual -- For FreeMarker 2.4pre1 - INCOMPLETE/OUTDATED!
HTML generated: 2009-03-29 13:52:16 GMT
Edited with XMLMind XML Editor
Here!