Powerful open-source software nvelocity

Source: Internet
Author: User

Background
Nvelocity (http://sourceforge.net/projects/nvelocity) is a. Net version transplanted from the velocity written in Java, is a super-powerful Java template system,. Net version of the current version of nvelocity is 0.42.
Nvelocity has a complete and rich template language (VTL, velocity template language). Almost all features of advanced languages can be found in the template engine language. (Such as process control statements, mathematical operations, relational and logical operators, function definitions, and comments)

What can nvelocity do?
Ability to quickly generate the requiredCode, SQL scripts, page files, and other text-based files
Fast generation, complete template language, and good flexibility
Easy to learn, open-source, and free to use
Formerly known as velocity (Java), with many users and comprehensive data

Purpose
When writing code, we can find that a lot of content does not need to be changed, but only a small part of the content is changed. For different objects, the content is different.
If we need to generate a variable document, do we need to copy the unchanged content in the Code, or open it to other files? Can we also have some simple control and reference for these content?

Simple Example (main rule: reference what is obtained starting with $, and the command starts with # For what to do)

# Set ($ Foo = false)
# If ($ Foo)
This is true
# Elseif ($ bar)
This is false
# Elseif (true)
This shoshould be followed by two blank lines
# End

# This is a single line comment

#*
This is a multi line comment
# If (
* #


# Set ($ user = "Jason ")
# Set ($ login = false)
# Set ($ COUNT = 5)

# If ($ user = "Jason ")
The user $ User Is logged in !
# End

# If ($ COUNT = 5)
The count is 5 !
# End

# If ($ login = false)
The user isn ' T logged in.
# End

# If ($ count! = 3)
\ $ Count is not equal to 3
# End

Variable description
In VTL, $ characters such as $ name must be added to the beginning of all variable identifiers, which can also be expressed in a more explicit way, such as $ {name }.
The variable identifier is mapped to the velocitycontext object to be introduced later. When the template engine processes a template, the variable name (such as name) is replaced with the value provided in velocitycontext.

C # code

 

Velocity. INIT ( " Nvelocity. Properties " );

Velocitycontext Context =   New Velocitycontext ();
Context. Put ( " List " , Names );

Template =   Null ;
Try
{
Template=Velocity. gettemplate ("Test. cs. VM");
}
Catch (Parseerrorexception pee)
{
System. Console. Out. writeline ("Syntax error:" +Pee );
}
If (Template ! =   Null )
{
Template. Merge (context, system. Console. Out );
}

 

Note
Single line comment
# This is a single line comment

Multi-line comment
#*
Thus begins a multi-line comment. Online visitors won't
See this text because the velocity templating engine will
Ignore it.
*#

Attribute or Method
$ Customer. Address
$ Purchase. Total

$ Page. settitle ("My home page ")
$ Person. setattributes (["strange", "weird", "excited"])

Command
# Set ($ primate = "monkey ")
# Set ($ monkey. Friend = "Monica ")

# Set ($ criteria = ["name", "Address"])
# Foreach ($ criterion in $ criteria)

# Set ($ result = $ query. Criteria ($ criterion ))
# If ($ result)
Query was successful
# End
# End

If/elseif/else
Foreach Loop

# If ($ Foo <10)
<Strong> go north </strong>
# Elseif ($ Foo = 10)
<Strong> go east </strong>
# Elseif ($ bar = 6)
<Strong> go south </strong>
# Else
<Strong> go west </strong>
# End

<Ul>
# Foreach ($ product in $ allproducts)
<Li> $ product </LI>
# End
</Ul>

Macro (more suitable for functions)
# The macro script element allows the template designer to define repeated segments in the VTL template. Velocimacros is useful in both complex and simple scenarios. The velocimacro below is only used to save the keys and reduce typographical errors. It introduces some nvelocity macro concepts.
It can contain parameters, which are placed behind the macro name and separated by spaces.

# Macro (d)
<Tr> <TD> </tr>
# End

# D ()

# Macro (callme $)
$
# End
 
# Callme ($ Foo. Bar ())

Include
# The include script element allows the template designer to include (import) local files, which will be inserted to the place where the # include command is defined. The file content is not rendered by the template engine.
# Include ("one.txt ")

Analysis
# The parse script element allows the page designer to import local files containing VTL. Velocity parses and renders the specified template.
# Parse ("me. VM ")

There are examples of template language syntax in the root directory nvelocity-*** \ test \ templates, in the nvelocity-*** \ examples directory, there is a simple example of how to use the template engine in C.
When using in. net, you need to apply twoProgramSet, nvelocity. dll, and commons. dll. These files are in the nvelocity-*** \ build directory.
You can add nvelocity. properties to configure some parameters of the template engine.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.