1. VTL: format
Although VTL in this guide often appears in the new row or has spaces, but the following VTL
#SET ($ imperial = ["Munetaka", "Koreyasu", "Hisakira", "Morikune"])
#foreach ($ SHOGUN IN $ IMPERIAL)
$ SHOGUN
#end
And the following is equally effective.
Send me #set ($ foo = ["$ 10 and", "a cake"]) # foreach ($ a in $ foo) $ A #END PLEASE.
Velocity's behavior is not affected by spaces, and the foregoing instructions can also be written:
Send Me
#set ($ foo = ["$ 10 and", "a cake"])
#foreach ($ a in $ foo)
$ a
#end
please.
or
Send Me
#set ($ foo = ["$ 10 and", "a cake"])
#foreach ($ a in $ foo) $ a
#end please.
Each way to write is the same.
2. Other features and miscellaneous
2.1. Mathematical characteristics
Velocity has some built-in mathematical features that can be used in the template using the SET instruction. The following consensus demonstrates the addition and subtraction and division operation:
#set ($ foo = $ bar 3)
#set ($ foo = $ bar - 4)
#set ($ foo = $ bar * 6)
#set ($ foo = $ bar / 2)
The result will be an integer when the division operation is performed. WHEN A Division Operation Is Performed, The Result Will Be An Integer. The remainder can be obtained by the model (%) operation.
#set ($ foo = $ bar% 5)
In Velocity, only integers can perform math operations; if the non-integer mathematical operation will be recorded, NULL is returned.
2.2. Range Operator
The range operator can be used with #set and #foreach statements. It helps to generate an integer's target array, and the range operator has the following structure:
[n..m]
N and M must be an integer or an integer can be generated. Regardless of M greater or less than N; if m is less than n, the range can count down. Here is an example of using the range operator:
First example
#foreach ($ foo in [1..5])
$ foo
#end
Second example
#foreach ($ BAR IN [2 ..- 2])
$ bar
#end
Third example
#set ($ arr = [0..1])
#foreach ($ I IN $ ARR)
$ I
#end
Fourth example
[1..3]
They produce output
1 2 3 4 5
2 1 0 -1 -2
0 1
[1..3]
When the range operator is used with #set and #fore, it is only generated together.
When the page designer is designed with the same size table, there is sometimes not enough data to populate, they will find that the range operator is very useful.
2.3. Advanced: Side!
When a reference is! The characters are in a quiet mode, and! Characters will be processed in a particular manner. Please pay attention to the difference between his and routine escape, the following situation / first!
#set ($ foo = "bar") $ /! foo
$ /! {foo}
$ //! foo
$ / //! foo
This will be processed into
$! foo
$! {foo}
$ /! foo
$ //! foo
Contrast routine escape, / prior to $:
/ $ foo
/ $! foo
/ $! {foo}
// $! {foo}
This is the result:
/ $ foo
/ $! foo
/ $! {foo}
/ bar
3. VelocimaCro miscellaneous
This section is a small FAQ for VelocimaS. This session will be updated from time to time, so please check new content.
Note: In this section, 'VelociMacro' will be abbreviated as 'VM'.
Q: Can I use the command Directive or VM as another VM parameter? For example: #Center (#bold ("Hello"))
A: No. The instruction cannot be used as a parameter of the instruction, and in most cases, as an actual application, VM is an instruction.
But there are some ways. A simple practice is to use dual quotes to process your content. So you can do this:
#set ($ stuff = "#bold ('Hello')")
#center ($ stuff)
Even can save a step:
#center ("#bold ('Hello')")
Note that in this example, the parameter is obtained inside the VM, not at that level called. In other words, the parameters of the incoming VM are all incorporated, and they are evaluated inside the incoming VM. So we can do this:
#macro (Inner $ FOO)
Inner: $ foo
#end
#macro (Outer $ foo)
#set ($ bar = "outerlala")
Outer: $ foo
#end
#set ($ bar = 'caltimela')
#outer ("#inner ($ bar)")
Here, the input will be:
Outer: Inner: Outerlala
Because "#inner ($ bar) is evaluated in #outer () inside, the $ bar value set in #outer () is the value used.
This is a intentional protection feature-parameter passed to the VM by name, so it can pass the things referenced by the status to VM, such as:
#macro (Foo $ Color)
#end
#foo ($ bar.rowcolor ())
RowColor () is repeated instead of once. To avoid this, you can call the outside of the VM, and then pass the value to the VM.
#set ($ color = $ bar.rowcolor ())
#foo ($ color)
Q: Can I register VM by #parse ()?
A: Currently, VelocimaS must first define it before it is used in the template. This means that #macro () claims that you should use VelocimaS before.
If you want #parse () a template containing the #macro () instruction, remember this is very important. Because #Parse () occurs at runtime, the parser is in parsing whether the template seems to be like a VM, so resolving a series of VM claims that it may not work very well. To avoid this, you can simply use VelocimaCro.library, allowing Velocity to load VM at startup. Q. What is VMA automatic loading (VelociMacro Autoreloading)?
A. This is an attribute that is used when developing, and when it is running:
Velocimacro.Library.AutoreLoad
The default is false. When set to True, set to false with the
Below is a simple setting configuration combination:
File.Resource.Loader.Path = Templates
File.Resource.Loader.cache = FALSE
Velocimaracro.library.AutoreLoad = TRUE
Be careful not to open in the production state (running).
4. Character series
One problem with developers is "How do I make a string series?" Is there a ' ' operator in Java?
In order to be referenced in tandem VTL, you have to "put together". The context you want to place together is very important, and the following is an example.
In the General "Stupid Measures" template:
#set ($ size = "big")
#set ($ Name = "BEN")
The Clock is $ SIZE $ Name.
The output will be: 'the clock is bigben'. Let's see more interesting things, for example, when you want to connect a string and pass it to a method, or set a new reference, you can do this:
#set ($ size = "big")
#set ($ Name = "BEN")
#set ($ clock = "$ size $ name")
The clock is $ clock.
The result is the same. As the last example, when you want to mix the "static" string to reference, you may need to use "form reference":
#set ($ size = "big")
#set ($ Name = "BEN")
#set ($ clock = "$ {size} tall $ name")
Now, the output will be 'the clock is bigtallben'.