Use of nvelocity

Source: Internet
Author: User

Some time ago, the. NET template engine nvelocity was used in the project. In order to deepen understanding of this template engine and facilitate its use in future projects, I would like to summarize it.

What is nvelocity?

Nvelocity is A. Net-based template engine ). It allows anyone to simply use the template language to referenceCodeDefined Object.
When nvelocity is applied to web development, the interface designer can work with. netProgramDevelopers develop a web site that follows the MVC Architecture synchronously. That is to say, the page designer can only focus on the display effect of the page, while the. Net program developer pays attention to the business logic encoding. Nvelocity separates. Net code from web pages, which facilitates the long-term maintenance of the web site and provides an optional solution beyond Aspx.
Nvelocity is far more powerful than Web site development. For example, it can generate SQL, postscript, and XML from templates. It can also be generated as an independent tool.Source codeAnd reports, or used as an integrated component of other systems. Nvelocity can also provide template services for many web development architectures ). Our system provides a template service that allows a web application to be developed with a real MVC model.

Usage of nvelocity:

1. First, reference the Assembly nvelocity. dll in the project.

In this way, nvelocity appears in the reference of the project.

 

2. Add the help class nvelocityhelp. CS file.

View code

1 Using Nvelocity;
2 Using Nvelocity. app;
3 Using Nvelocity. context;
4 Using Nvelocity. runtime;
5 Public Class Nvelocityhelper
6 {
7 Private Velocityengine velocity = Null ;
8 Private Icontext context = Null ;
9
10
11 ///   <Summary>
12 /// No parameter Constructor
13 ///   </Summary>
14 Public Nvelocityhelper ()
15 {
16 Init ();
17 }
18
19 ///   <Summary>
20 /// Nvelocity Module
21 ///   </Summary>
22 Public Void Init ()
23 {
24 // Create a velocityengine Instance Object
25 Velocity = New Velocityengine ();
26 Velocity. INIT ();
27 // Assign values to template Variables
28 Context = New Velocitycontext ();
29 }
30
31 ///   <Summary>
32 /// Nvelocity Module
33 ///   </Summary>
34 ///   <Param name = "templatepath"> Template path </Param>
35 Public Void Init ( String Templatdir)
36 {
37 // Create a velocityengine Instance Object
38 Velocity = New Velocityengine ();
39
40 Extendedproperties props = New Extendedproperties ();
41
42 Props. addproperty (runtimeconstants. resource_loader, " File " );
43 Props. addproperty (runtimeconstants. file_resource_loader_path, httpcontext. Current. server. mappath (templatdir ));
44 Props. addproperty (runtimeconstants. input_encoding, " UTF-8 " );
45 Props. addproperty (runtimeconstants. output_encoding, " UTF-8 " );
46
47 Velocity. INIT (props );
48 // Assign values to template Variables
49 Context = New Velocitycontext ();
50 }
51
52 Public String Convertbyfile ( String Filename)
53 {
54 Stringwriter writer = New Stringwriter ();
55 Template template = velocity. gettemplate (filename, " UTF-8 " );
56
57 Template. Merge (context, writer );
58
59 Return Writer. getstringbuilder (). tostring ();
60 }
61
62 ///   <Summary>
63 /// Assign values to template Variables
64 ///   </Summary>
65 ///   <Param name = "key"> Template Variables </Param>
66 ///   <Param name = "value"> Template variable value </Param>
67 Public Void Put ( String Key, Object Value)
68 {
69 Context = context ?? New Velocitycontext ();
70 Context. Put (Key, value );
71 }
72
73 ///   <Summary>
74 /// Output HTML code parsed Based on the template content
75 ///   </Summary>
76 ///   <Param name = "templatecontent"> </param>
77 Public Void Display ( String Templatecontent)
78 {
79 Stringwriter writer = New Stringwriter ();
80
81 Try
82 {
83 Velocity. Evaluate (context, writer, Null , Templatecontent );
84 Httpcontext. Current. response. Clear ();
85 Httpcontext. Current. response. Write (writer. getstringbuilder (). tostring ());
86 }
87 Catch (Exception ex)
88 {
89 Httpcontext. Current. response. Clear ();
90 Httpcontext. Current. response. Write (ex. Message. tostring ());
91 }
92
93 Httpcontext. Current. response. Flush ();
94 }
95
96 ///   <Summary>
97 /// Returns the parsed HTML content.
98 ///   </Summary>
99 ///   <Param name = "templatecontent"> </param>
100 ///   <Returns> </returns>
101 Public String Convert ( String Templatecontent)
102 {
103 Stringwriter writer = New Stringwriter ();
104 Velocity. Evaluate (context, writer, Null , Templatecontent );
105
106 Return Writer. getstringbuilder (). tostring ();
107 }
108 }

 

 

3. In the action in the controller, call the method to replace the template engine variables on the page, for example, convert ("Index") in the index below. Here, membershipcontroller inherits the following membershipbasecontroller

Membershipcontroller. CS

Public actionresult index (int id, int themeid = 0, int styleid = 0)
{
This. membershipid = ID;
This. _ themeid = themeid;
This. _ styleid = styleid;
Return convert ("Index ");
}

 

4. The method for replacing the template engine variables on the page is defined in membershipbasecontroller. CS.

Public actionresult convert (string code, string detailname = "", string detailkeywords = "", string detaildesc = "")

// Introduce the variable lresult to determine whether the user is the latest enterprise (whether the user is a trusted user)

Bool lresult = This. islatestjoincompanylogo (membershipid );
// Introduce the variable vresult to determine whether the user is a VIP user (Enterprise VIP or Government VIP)
Bool vresult = This. isvipuser (membershipid );

// Introduce two variables to the nvelocity engine to determine whether it is a trusted user or a VIP user on the front-end page.

The first one of the put method is the key and the last one is the value. On the front-end page, we use the key.
Velocity. Put ("lresult", lresult );
Velocity. Put ("vresult", vresult );

Return content (velocity. Convert (content ));

 

5. The last code above velocity. Convert (content) calls the convert method of nvelocityhelper. CS

/// <Summary>
/// Return the parsed HTML content
/// </Summary>
/// <Param name = "templatecontent"> </param>
/// <Returns> </returns>
Public String convert (string templatecontent)
{
Stringwriter writer = new stringwriter ();
Velocity. Evaluate (context, writer, null, templatecontent );

Return writer. getstringbuilder (). tostring ();
}

6. reference the template variables defined in the background in the HTML code of the front-end page

# If ($ lresult) <span style = "float: Left"> </span> # End

# If ($ vresult) # End

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.