Variables defined in the freemarker template and the freemarker Template
Three types of variables can be defined in the template:
- Simple variable: it can be accessed from any location in the template, or from the template introduced using the include command. You can use the assign or macro commands to create or replace these variables.
- Local variables: they can only be set in the macro definition body and only visible in the macro. The life cycle of a local variable is only a macro call process. You can use the local command to create or replace local variables in the macro definition body.
- Cyclic variables: cyclic variables are automatically created by commands (such as list), and they are only valid in the start and end labels of the commands. Macro parameters are local variables rather than circular variables.
Example: Use assign to create and replace variables
<# Assign x = 1> <# -- create variable x -- >$ {x} <# assign x = x + 3> <# -- replace variable x -- >$ {x}
14
Local variables also hide (not overwrite) simple variables with the same name. Loop variables also hide (not overwrite) local variables with the same name and simple variables. For example:
<# Assign x = "plain"> 1. $ {x} <# -- the common variable --> <@ test/> 6. $ {x} <# -- the value of the common variable is not changed --> <# list ["loop"] as x> 7. $ {x} <# -- now the cyclic variable hides the common variable --> <# assign x = "plain2"> <# -- replace the common variable, hiding does not work here --> 8. $ {x} <# -- it still hides common variables --> </# list> 9. $ {x} <# -- new value of a common variable -->
<# Macro test> 2. $ {x} <# -- here we still see common variables --> <# local x = "local"> 3. $ {x} <# -- now the local variable hides it --> <# list ["loop"] as x> 4. $ {x} <# -- now the cyclic variable hides the local variable --> </# list> 5. $ {x} <# -- now you can see the local variable --> </# macro>
1. plain 2. plain 3. local 4. loop 5. local6. plain 7. loop 8. loop9. plain2
Note that the setting of the loop variable is created by calling the command (the <list...> label in this example ). There is no other way to change the value of a loop variable (that is, you cannot use a definition command to change its value .). As shown in the preceding example, a loop variable can be used to hide another variable.
Sometimes a variable hides a variable of the same name in the data model. However, if you want to access a variable in the data model, you can use the special variable globals. For example, assume that we have a variable named "user" and its value is "BigJoe" in the data model.
<# Assign user = "Joe Hider" >$ {user} <# -- print: Joe Hider -- >$ {. globals. user} <# -- print: Big Joe -->
Freemarker custom Variables
...... As long as it is an individual, we must launch it without any means, so that this good post will not sink to us,
How is the global variable in the freemarker template?
Please query the statics usage of freemarker