Application of the nvelocity template engine in the project

Source: Internet
Author: User

Nvelocity is A. Net-based template engine ). It allows anyone to simply use the template language to reference objects defined by. Net code. We will not mention other nvelocity interpretations and usage here.

Specific usage:

1. Define the velocity auxiliary class (velocityhelper. CS)

Provide download, of course, this helper class needs to add nvelocity. dll reference.

2. Structure Description of the ASPX page and template

This is determined based on your preferences! I put it in this way to better understand my own structure!

Place all *. VM templates in the template

Where: The template where the Browse folder is placed on the foreground page, and the manage folder is placed on the background page template. Note that the default folder under this folder is only an identifier that distinguishes the foreground and background templates.

Manage folder placement management *. ASPX page

The page in the manage folder, for example, the template called by linktypeedit. aspx, is template/defalut/linktypeedit. VM. The names are matched in sequence. Of course, the corresponding names are not required.

Place the root directory on the foreground *. ASPX page

The template called on the front-end page is the Template under template/Browse/defalut/. It also corresponds to the Management page by name.

3. Call and assign values

Add the velocityhelper. CS helper class to the project.

Let's take defalut. aspx as an example. First, edit template/Browse/defalut/Default. VM, which is the template of our homepage.

We can see that the template variables are $ pagetitle and $ hello in two places. In this way, you must use the engine template in default. aspx to assign values to variables and display templates.

Next let's take a look at defalut. ASPX page, because the display is completely using the template, so default. the ASPX page does not need anything, but only leaves a line of @ page. The background code is in the page_load event and we need to perform the corresponding operations!

Protected void page_load (Object sender, eventargs e ){

// Create a helper class

Luz. Web. util. velocityhelper VL = new Luz. Web. util. velocityhelper ();

// Template location

String templatepath = "/template/Browse/defalut /";

// Initialize the engine through the template location

Vl. INIT (templatepath );

// Assign values to variables. The first parameter is the name of the variable, that is, the name of the variable you wrote in the template. Note that there is no "$"; the second parameter is the value, object type.

Vl. putset ("pagetitle", "Hi I am title ");

Vl. putset ("hello", "Hello nvolecity ");

// Display template

Vl. Display ("Default. VM ");

}

Protected void page_load (Object sender, eventargs e) {// create a helper class Luz. web. util. velocityhelper VL = new Luz. web. util. velocityhelper (); // template location string templatepath = "/template/Browse/defalut/"; // initialize the engine in the template location. init (templatepath); // assign values to variables. The first parameter is the variable name, that is, the name of the variable you wrote in the template. Note that no "$" is specified "; the second parameter is the value, object type. Vl. putset ("pagetitle", "Hi I am title"); Vl. putset ("hello", "Hello nvolecity"); // display the template. display ("default. VM ");}

Run the following command to see if the template has been displayed and $ pagetitle and $ Hello have been replaced with the value assigned to it in the page_load event. Of course, you can assign values to any type, including list generics (using # foreach traversal in the template.

Application of the nvelocity template engine in the project

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.