Variables defined in the freemarker template and the freemarker Template

Source: Internet
Author: User

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
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.