l Macro and transducer variables are two different types of user-defined instructions. The difference between them is that the macro uses the Macro command definition in the template, and the converter is defined by the program outside the template, only the macro
l Basic usage
Ø Macro is the template piece associated with a variable to use this variable through the user-defined instructions in the template, and below is an example:
<#macro greet>
Hello Joe! font>
# macro>
Ø When using macro variables as user-defined instructions, use @ replacing the FTL tag in #
<@greet> @ Greet>
Ø If there is no body content, you can use:
<@ Greet />
l parameter
Ø You can define parameters after the macro command, such as:
<#macro greet person>
Hello $ {person}! font>
# macro>
Ø You can use this macro variable:
<@Greet Person = "fred" /> and <@greet person = "batman" />
The output result is:
Hello Fred! font>
And Hello Batman! font>
Ø Macro parameters are FTL expressions, so the following code has different meaning:
<@greet penson = fred />
Ø This means passing the value of the FRED variable to the Person parameter, which is not only a string, but also other types, even complex expressions.
Ø Macro can have multiple parameters, below is an example:
<#macro Greet Person Color>
Hello $ {person}! font>
# macro>
Ø Can use this macro variable:
<@Greet Person = "fred" color = "black" />
Ø The order of which parameters is independent, so the following is equivalent:
<@greet color = "black" Person = "fred" />
Ø You can only use the parameters defined in the macro command, and assign all parameters, so the following code is wrong:
<@Greet Person = "Fred" color = "black" background = "green" />
<@Greet Person = "Fred" /> Ø Specify the default value when defining the parameters, such as:
<#macro greet person color = "black">
Hello $ {person}! font>
# macro>
Ø This <@greet person = "fred" /> is correct
Ø Macro parameters are local variables, can only be valid in macro definitions
l Nesting content
Ø User definition instructions can have nested content, use the <#NESTED> instruction to execute the template piece between the instruction start and the end tag
Ø Example:
<# macro border>
<#NESTED>
TR> TD> table>
# macro>
This macro variable is used this:
<@Border> The bordered text @ border>
Output results:
The bordered text
TR> TD> table>
Ø <#NESTED> The instruction can be called multiple times, for example:
<#macro do_thrice>
<#NESTED>
<#NESTED>
<#NESTED>
# macro>
<@do_thrice>
Anything.
@ DO_THRICE>
Output results:
Anything.
Anything.
Anything.
Ø Nested content can be a valid FTL, the following is a somewhat complicated example:
<@Border>
<@do_thrice>
<@ Greet Person = "Joe" />
@ DO_THRICE>
ul>
@ border>
Output results:
Hello Joe! font>
Hello Joe! font>
Hello Joe! font>
ul>
TR> TD> table>
Ø The local variables in the macro definition are invisible to nested content, such as: <# macro refeat count>
Ø User definition instructions can have loop variables, which are commonly used to repeat nested content, and the basic usage is: As the actual value of the cyclic variable as the parameter of the NESTED instruction, when calling the user-defined instruction, the parameters start tagged in <@ ...> Next specified the name of the loop variable
Ø Example:
<#macro refeat count>
<#list 1..count as x>
<#NESTED X, X / 2, X == Count>
# list>
# macro>
<@repeat count = 4; C, HALFC, LAST>
$ {C}. $ {halfc} <# if last> last! # f>
@ repeeat>
Output results:
1. 0.5
twenty one
3. 1.5
4. 2 Last!
Ø The number of specified loop variables and the different definition instructions start tag different will not have problems
n: Specify a loop variable at the time of call, then more specified values are invisible
n When calling, specify a loop variable, excess loop variables will not be created
(2) Define variables in the template
l There are three types of variables defined in the template:
Ø PLAIN variable: You can access anywhere in the template, including the template inserted using the include instruction, create and replace the Assign instruction
Ø Local variable: Valid in the macro definition, create and replace the Local instruction
Ø Cycle variable: only exist in the nested content of the instruction, automatically created by the command (such as list); the parameters of the macro are local variables, not a loop variable
l Local variables hide (rather than overwriting) The same name is the plain variable; the loop variable hides the local variables and plain variables of the same name, the following example:
<#ssign x = "plain">
1. $ {x} <# - we see the plain var. Here ->
<@ TEST />
6. $ {x} <# - The value of plain var. Was at change ->
<#list ["loop"] as x>
7. $ {x} <# - Now the loop var. Hides The Plain Var. ->
<#ssign x = "plain2"> <# - replace the plain var, hiding does not mat here -> 8. $ {x} <# - it still hides the plain var. ->
# list>
9. $ {x} <# - The new value of plain var. ->
<#macro test>
2. $ {x} <# - West See The Plain Var. Here ->
<#local x = "local">
3. $ {x} <# - Now the local var. Hides it ->
<#list ["loop"] as x>
4. $ {x} <# - now the loop var. Hides The Local Var. ->
# list>
5. $ {x} <# - now we see the local var. Again ->
# macro>
Output results:
PLAIN
2. Plain
3. Local
4. loop
5. Local
6. Plain
7. loop
8. loop
9. Plain2
l The internal cycle variable hides the external loop variable of the same name, such as:
<#List ["loop 1"] AS X>
$ {x}
<#List ["loop 2"] AS X>
$ {x}
<#List ["loop 3"] AS X>
$ {x}
# list>
$ {x}
# list>
$ {x}
# list>
Output results:
LOOP 1
Loop 2
Loop 3
Loop 2
LOOP 1
l The variables in the template hide (not overwriting) the same name variable in the data model. If you need to access the same name variable in the data model, use special variables Global, the following example assumes that the value in the data model is Big Joe:
<#ssign user = "joe hider">
$ {user} <# - prints: joe hider ->
$ {. globals.user} <# - prints: big joe ->
(3) Namespace
L usually, only one name space, called the primary name space
l In order to create a reusable macro, a converter, or a collection of other variables (typically known as the library), you must use multiple word space, its purpose is to prevent the same name conflict
l Create a library
Ø The following is an example of a creation library (assuming saved in lib / my_test.ftl):
<#macro copyright date>
Copyright (c) $ {date} julia smith. All rights reserved.
Email: $ {mail} p>
# macro>
<#ssign mail = "jsmith@acme.com">
Ø Import the library to the template using the Import Directive, FreeMarker creates a new namespace for the imported library, and can access variables in the library through the hash variables specified in the Import instruction: <# import "/lib/my_test.ftl" as my>
<#ssign mail = "fred@acme.com">
<@ my.copyright date = "1999-2002" />
$ {my.mail}
$ {mail}
Output results:
Copyright (C) 1999-2002 Julia Smith. All Rights Reserved.
Email: jsmith@acme.com p>
jsmith@acme.com
Fred@acme.com
It can be seen that the two identical variables used in the example are not conflict because they are located in different namespaces.
l You can create or replace the variables in the imported namespace using the Assign instruction. Here is an example:
<#import "/lib/my_test.ftl" as my>
$ {my.mail}
<#ssign mail = "jsmith@other.com" in my>
$ {my.mail}
l Output result:
jsmith@acme.com
Jsmith@other.com
l Variables in the data model can be seen, including different namespaces, the following is a modified library:
<#macro copyright date>
Copyright (C) $ {date} $ {user}. All rights reserved. p>
# macro>
<#ssign mail = "$ User }@acme.com">
l Suppose the value of the USER variable in the data model is fred, then the following code:
<#import "/lib/my_test.ftl" as my>
<@ my.copyright date = "1999-2002" />
$ {my.mail}
l Output result:
Copyright (C) 1999-2002 Fred. All Rights Reserved. P>