1. Semantic Getting Literal
VTL special characters, such as $ and #, do this, so you must be extra care when using these self-contained in the template. This section tells the escape of $.
1.1. Currency
We wrote the sentence "I BOUGHT A 4 LB. Sack of Potatoes At The Farmer's Market for Only $ 2.50!" This is nothing. However, as mentioned earlier, the VTL identifier always begins with uppercase or lowercase letters, so $ 2.50 will not be wrong in the reference.
1.2. Side essential VTL reference
The problem will appear because Velocity will have a potential conflict. Ever-special characters are the best way to handle special characters for VTL template species, and they can do it with a reverse slope.
foo
/ foo
If Velocity encounters a $ Email reference in the VTL template, he will find the corresponding value in the context. Here, the output will be foo because $ EMAIL is defined. If $ email is not defined, the output will be $ email.
Suppose $ email is defined (for example, with value foo), but you want to output $ EMAIL. There are several ways to do this, not the simplest is to use escape characters.
## The folowing line defines $ Email in this template:
#set ($ email = "foo")
Will output
foo
/ foo
Note: / Binds the left side of $. From the principle of binding principles, /// $ Email is interpreted as // $ email. And the above example compares the following example, here $ EMAIL is undefined.
Output
Note that Velocity handling definitions and undefined references are different. The following set instruction sets $ foo to value gibbous ..
#set ($ foo = "gibbous")
$ moon = $ foo
Output will be $ moon = Gibbous
- Here $ moon is used as a literal output because he is not defined. Gibbous will output in the location of $ foo.
We can also escape the VTL directive, which will be in the command.
2. Case Substitution
Now you can roughly understand the reference, you can use them in the template. Velocity has adopted a lot of Java principles, and template designers will find very easy to use. E.g:
$ foo
$ foo.getbar ()
## Is The Same AS
$ foo.bar
$ Data.Getuser ("Jon")
## Is The Same AS
$ Data.user ("Jon")
$ DATA.GETREQUEST (). getServerName ()
## Is The Same AS
$ data.Request.servername
## Is The Same AS
$ {data.request.servername}
This example shows some other usage of references. Velocity draws on Java's self-provincial and component bean features to solve the problem of reference names as objects and object methods in context. You can insert reference and evaluation anywhere in your template.
Velocity, modeling on Sun Microsystems defined Bean specification is sensitive; developers strive to capture and correct user errors that may occur. When the method getfoo () is quoted by $ bar.foo in the template, Velocity tries $ getfoo. If he fails, he will try to get $ getfoo. Similarly, when a template is referenced to $ bar.foo, Velocity will try $ getfoo () first, then try getfoo (). Note: The problem with reference sample variables in the template is still not resolved. Only the reference equivalent to the javabean getter / setter method is solved. (For example, $ foo.name solves the reference to the getName () sample method of class foo, but cannot reference a public instance variable name of FOO).
3. Directive
Because the instruction (using the script is effective to control the output of Java code) Allow the page designer to truly focus on the appearance and content design, the reference allows the template designer to generate dynamic content for the web page.
3.1. # Set
#set instructions are used to set the corresponding value for the reference. Value can be sent to variable references or attribute reference, and assignment to enclose in parentheses.
#set ($ primate = "monkey")
#set ($ Customer.Behavior = $ primate)
The left side of the assignment must be a variable application or attribute reference. The right side can be one of the following types:
Variable reference
Literal string
Property reference
Method reference
Literal
Array list
These examples demonstrate each of the above:
#set ($ monkey = $ bill) ## Variable Reference
#set ($ monkey.friend = "monica") ## string literal
#set ($ monkey.blame = $ whitehouse.leak) ## Property Reference
#set ($ monkey.plan = $ spindoctor.Weave ($ Web)) ## Method Reference
#set ($ monkey.number = 123) ## Number Literal
#set ($ monkey.say = ["not", $ my, "fault"]) ## arraylist
Note: In the last example, items defined in square brackets [.] Can be accessed by the ArrayList class definition. For example, you can use $ monkey.say.get (0) to access the first element described above.
On the right, you can also be a simple arithmetic expression:
#set ($ VALUE = $ FOO 1)
#set ($ value = $ bar - 1)
#set ($ VALUE = $ foo * $ bar)
#set ($ VALUE = $ FOO / $ BAR)
If the right is an attribute or method reference, the value is null, and he will not assign a value to the left. It is impossible to remove an existing reference from the context through this mechanism. This may confuse the novice of Velocity. E.g:
#set ($ Result = $ query.criteria ("name"))
The Result of The First Query IS $ Result
#set ($ Result = $ query.criteria ("address"))
The Result of The Second Query IS $ Result
If, $ query.criteria ("name") puts back the string "Bill", and $ query.criteria ("address") returns NULL, the above VTL will be explained as: The Result of the first query is Bill
The Result of the Second Query Is Bill
This will often give those who want to build a #foreach loop to try to set a newcoming newcomer to set a reference to the newcomer, and now test it immediately through the #IF instruction. E.g:
#set ($ criteria = ["name", "address"])
#foreach ($ criterion in $ criteria)
#set ($ Result = $ query.criteria ($ criterion))
#if ($ Result)
Query Was Successful
#end
#end
In the above example, depending on the value of $ Result to determine if the query is probably not a broad practice. When the $ RESULT is set to #set (added to the context), he can no longer be set to NULL (removed from the context).
Our solution is to preset $ Result is false. Then if there is a $ query.criteria () call failed, you can check it.
#set ($ criteria = ["name", "address"])
#foreach ($ criterion in $ criteria)
#set ($ result = false)
#set ($ Result = $ query.criteria ($ criterion))
#if ($ Result)
Query Was Successful
#end
#end
Not like other Velocity instructions, #set instructions do not have a #nd statement.
3.2. Language string
When using the #set directive, the literal string in dual quotes will be resolved and reinlease, as shown below:
#set ($ DIRECTORYROOT = "WWW")
#set ($ TemplateName = "INDEX.VM")
#set ($ Template = "$ DIRECTORYROOT / $ TEMPLATENAME")
$ Template
The output will be:
Www / index.vm
However, when the literal characters are spliced in single quotes, he will not be parsed:
#set ($ foo = "bar")
$ foo
#set ($ blang = '$ foo')
$ blargh
The output is:
Bar
$ foo
By default, use single quotes to render unrestricted texts in Velocity. This feature can be changed by editing StringLITerals.Interpolate = false in velocity.properties.
3.3. Condition
3.4. IF / Elseif / Else
The #IF instruction in Velocity allows the text to contain text when the IF condition is real in the page. E.g:
#if ($ foo)
velocity! strong>
#end
Variable $ FOO first request value to determine if it is true. In both cases: (i) $ foo is a logical variable and has a true value, or (ii) value is not empty. Remember that the Velocity context includes only objects, so when we say "Boolean" 'Boolean', he will be expressed as "Boolean Class). This is true that even if it is a method of returning a Boolean type - the provincial architecture will return a Boolean class with the same logic value. If the value is true, the content between #IF and #END statements will output. In this case, if $ foo is true, the output will be "velocity!". Conversely, if the $ foo has a NULL value, or logic fake, the statement evaluation is false, there is no output.
A #ELSEIF or #ELSE item can be used in the #IF statement. Note that the Velocity Template engine will stop when the first is true expression. In the following example, it is assumed that the $ foo has a value of 15 and $ bar is equal to 6.
#if ($ FOO <10)
go north strong>
#ELSEIF ($ foo == 10)
Go East strong>
#ELSEIF ($ bar == 6)
Go South strong>
#ELSE
Go West strong>
#end
In this example, $ foo is greater than 10, so two comparison failures. Next, $ BAR and 6 have been true, so the output is Go South.
Please note that the value of Velocity is constrained in the current value - Other types will be evaluated as false. There is only one exception to be equal to '==', and the Velocity requires the object on both sides of the same type.
3.5. Relationship and logic operator
Velocity uses equation operators to determine the relationship between the two variables. Here is a simple example to demonstrate how to use the equality operator:
#set ($ foo = "deoxyribonucleic acid")
#set ($ bar = "ribonucleic acid")
#if ($ foo == $ bar)
In this case it's clear the aren't Equivalent. So ...
#ELSE
They area not equivalent and this will be the output.
#end
Velocity also has logical AND, OR and NOT operators. For further information, please see the VTL Reference Manual VTL Reference Guide. Here are some examples of how to use logical operators:
## logical and
#if ($ FOO && $ BAR)
this and that strong>
#end
In the example #if () instructions are really true in $ foo and $ bar. If $ foo is fake, the expression is also false; and $ bar will not be evaluated. If $ foo is true, the Velocity Template engine will continue to check the value of $ bar; if $ bar is true, the entire expression is true. And output this and That. If $ bar is a fake, there will be no output because the entire expression is false.
Logic OR works the same, unique exception is one of the expressions to be evaluated to determine if the entire expression is true. Please see the example below: ## logical or
#if ($ foo || $ bar)
this or what strong>
#end
If $ foo is true, the Velocity template engine does not need to look at the value of $ bar, regardless of whether $ bar is true, true expression is true, therefore output this or what. If $ foo is fake, $ bar must check its value. In this case, if $ bar is also false, the expression will be false, without any output. Of course, if $ BAR is true, the true expression is true, output this or what.
For logical NOT operators, there is only one operand:
## logical not
#if (! $ foo)
not that strong>
#end
Here, if $ foo is true, $ foo evaluation is false, no output. If $ foo is fake, $ foo evaluates the value, outputs Nothat. Be careful, don't quietly quote Quiet Reference $! Foo confuse them is completely different.