1. nvelocity Definition
Nvelocity is A. Net-based template engine. It defines a simple template language to reference from the HTML page by. netCodeDefine objects and reference external files. So that the interface designer and. netProgramDevelopers are basically separated. The following summarizes the commonly used template tags on the Internet and my understanding of nvelocity.
I. Introduction to common nvelocity Functions
1. Define variables on the page and perform simple operations.
2. obtain reference to the background program object on the page.
3. iterate the object set on the page.
4. Obtain the attributes and methods of the object on the page.
5. Support for logical interpretation statements.
6. References to external files.
7. parsing external files.
Ii. How nvelocity works
You can use. Net reflection. The following are simple steps for nvelocity to implement iterative entity classes:
1. Define the people class and have the name and sex attributes. Represents a person.
2. To list characters on the page, enter the following code:
# Foreach ($ P in $ PS)
<P> welcome: $ P. Name </P>
# End
3. Obtain the character list and save it to _ list. And specify the string "Ps" corresponding to _ list in the page.
4. Read the template file in text format and match # foreach... # End segment. If it matches, it will continue to match $ X in.
$ XX, respectively, records the characters used to save the set and individual items. This time is "P" and "Ps ".
5. Use the getproperties () method of the type object to obtain all the attributes of each item in _ list.
# Foreach # Replace the $ P + attribute name with the current attribute value of the current object in the end segment cyclically. Of course, if you want to call the object method, you can also get it in the same way.
III. Basic syntax
1. special characters
A. "#" indicates what to start.
B. "$" indicates what is used for obtaining.
C. "#": single-line comment.
D. "# *... * # ": Multi-line comment.
2. Keywords
A. Set: start to do something, such as defining variables and assigning values to variables.
B. foreach: iterative statement
C. If: Condition judgment statement
D. elseif
E. Else
F. include: References to external files. the start position is the template path.
G. parse: References to external files and resolves them in nvelocity mode.
H. Macro: Create a macro. You can repeat something, similar to a method.
I. Even: double execution
J. Odd: singular execution
K. Each: run every time
(Note: all variables cannot be used before they are undefined (because we are used to global variables ).. Except for objects defined in the net background. The template language is case sensitive. All keywords MUST be lower-case, and the list above is not full)
Iv. Example
1. Use variables on the page
Define the variable: # Set ($ A = "CNF ")
Reference variable: Welcome: $
Define variable: # Set ($ A = 1)
Operation: # Set ($ A = $ A + 1)
Output: $ A ##: 2
Operation: # Set ($ A = $ A * 5)
Output: $ A ##: 10
(Note: from the above, we can see that the replacement sequence of nvelocity is basically the same as that of the. NET program code. If it is placed in the foreach statement block, it can be accumulated. Use the if statement to obtain the row number and perform special processing on the content of the special row number. All variables cannot be used before they are undefined, except for. Net background objects)
2. Use the condition judgment Statement on the page
# If ($ P. strsex = "")
# Set ($ sex = "Ms ")
# Elseif ($ P. strsex = "male ")
# Set ($ sex = "Mr ")
# Elseif ($ P. strsex = "")
# Set ($ sex = "demon ")
# Else
# Set ($ sex = "monster ")
# End
(Note: It can be nested in the foreach statement block for special display and processing of each list object .)
3. Create a macro and use it as a method.
Create: # macro (add $ A $ B)
# Set ($ c = $ A + $ B)
<P> final result: $ C </P>
# End
Call: # Add (1 2)
(Note: There are three initialization methods for the template engine. One is the template file content, and the other is the template file address. The results showed that the content of the template file seemed to be a problem when applying macros. In addition, if a judgment statement is added to the macro, recursive calling can be implemented .)
4. Object Method
Define the variable: # Set ($ STR = "CNF ")
Call method: $ Str. substring (0, 1)
Output: c
Define variable: # Set ($ A = 123)
Call method: $ A. GetType ()
Output: system. int32
(Note: whether it is an object defined by. Net code or a variable defined by the designer on the page, the method and attributes of the object can be used, which is very powerful .)
5. Use even and odd to simplify the code.
As mentioned above, the IF statement can be used to create different styles for each row in the list. However, if you only need to distinguish between single and double rows, you can use even and odd to simplify the code. As follows:
# Foreach ($ P in $ PS)
# Even
<P> double row: $ P. strname </P>
# Odd
<P> single row: $ P. strname </P>
# End
(Note: when these two keywords are used, the problem is the same as that of the macro creation, that is, when the template is initialized
If the template file content is initialized when the engine is running, a problem may occur)
6. Reference external files
Both include and parse can introduce external files. The difference is that parse will parse external files according to the nvelocity template language. That is to say, if the current template is introduced, an endless loop will occur.
7. Use the foreach statement
The foreach statement has been listed multiple times above, and I believe it is useful. A set of objects is displayed cyclically. For example: # foreach ($ P in $ PS), where $ PS must correspond to the specific object class name in the background code, $ p represents one of $ ps. As mentioned above, $ P can call attributes and methods of object classes.
(Note: # The foreach statement must end with # End)
8. Create an array
Create: # Set ($ list = ["male", "female"])
Traversal: # foreach ($ item in $ List)
<P> List member: $ item </P>
# End
Output: List member: Male
List member: Female
5. I hope you can continue to improve this article.