An important reason I like to use nvelocity view engine in ASP. NET MVC is nvleocity's simple and easy-to-use syntax. Almost # Set, # If, # foreach can be used to form a complete application. The following describes the syntax and Common commands in nvelocity.
By default, nvelocity resolution is case-insensitive. Of course, you can set runtime. Strict. Math = true to use the strict resolution mode.
Variable reference: $ [! ] [{] [A. Z, A. Z] [A. Z, A. Z, 0. 9,-, _] [}].
In nvelocity, variables are referenced starting with $ with a variable name. When used! When the variable value is null, an empty string is displayed. For example, if $ article is empty, "$ article" is displayed, while $! Article is displayed as "". {} Is the variable name restriction. Sometimes there is a string after the variable name, which requires. For example, if you want to reference $ article in $ articleshow, you only need to change it to $ {Article. In fact, nvelocity will change to this mode after parsing the entire template.
Attribute reference: $ [{] [.. z, .. z] [.. z, .. z, 0 .. 9,-, _] *. [.. z, .. z] [.. z, A-Z, 0 .. 9,-, _] * [}].
For example, $ article. Title or $ {Article. Title }.
Reference to a method: $ [{] [.. z, .. z] [.. z, .. z, 0 .. 9,-, _] *. [.. z, .. z] [.. z, .. z, 0 .. 9,-, _] * ([optional parameter list...]) [}].
For example, $ article. getlistbytitle ('nvelocity ') or $ {Article. getlistbytitle ('nvelocity ')}. In fact, you can use $ article. get_title () to obtain the object property value.
Common commands:
Value assignment command # Set: # [{] Set [}] ($ ref = [", '] Arg [",']).
Example: $ article. title = 'nvelocity ', $ article. categories = [1, 2, 3]. Of course, you can also use a complex expression on the right: $ article. title = $ otherArticle. title. substring (0, 3), arithmetic expression: $ article. page = 4/3 and so on. You can also use $ article. set_title ('nvelocity ') to assign values to attributes ').
Condition command # If: # [{] If [}] ([condition]) [Output] [# [{] elseif [}] ([condition]) [Output] * [# [{] else [}] [Output] # [{] end [}].
The condition can be the review expression of the returned bool. For example: # If ($ article. Total> 1) $ article. Title # else has no data # End.
Loop command # foreach: # [{] foreach [}] ($ refinarg) Statement # [{] end [}].
For example: # foreach ($ article in $ articles) $ article. Title # End.
Reference static resource commands # include: # [{] include [}] (ARG [arg2... argn]).
For example, # include ('tmp. js') inserts the content of the TMP. js file into the current stream. Of course, you can use the expression # include ($ article. URL ).
Reference and parse the resource command # parse: # [{] parse [}] (ARG ).
Example: # parse ('tmp. js'), different from # include, if TMP. the JS file contains nvelocity commands, which process the variables and insert the results to the current stream.
Stop command # Stop: # [{] Stop [}].
When nvelocity resolves to this command, the parsing process is stopped. Debugging by users.
Calculation command # evaluate: # [{] evaluate [}] (ARG ).
For example: # evaluate ('$ article. title'), $ article. Title is output at the moment.
The above are common nvelocity commands. The next article will show how to use nvelocity macro and how to develop user ctictive.