A long time not hair technical aspects of the dynamic, today bored to send a piece about nvelocity technical article, this technology comes from the Java Open source project velocity, relatively easy to use, the other I also just introduced, did not listen to the article at the end of the introduction, the following we will combat it ~
Let's go straight to the simplest way, one sentence:
Dntnvelocityhelper.nvelocitytemplate (context. Request.mappath ("~/nvelocity/templates/"), Context, "templater_index.dnt", "Utf-8", DIC);
A lot of people have egg ache, you this unthinking a word to make wool? No hurry, let's start with the basics, that's just the last of the simplest states.
Speaking of Nvelocity, then the official website first put out: http://www.castleproject.org/download/, what is the use of this nvelocity, why use him?
Actually let me say this compares the egg ache, you just remember 2 points on the line: one: foreground and background work separation. Second: You do not have to manually read the string to make cumbersome substitutions.
Someone might ask, isn't there a motherboard page? Feeling pretty much? Well, you can understand that velocity is the meaning of speed in words, if it's the same efficiency. It turns out that they are not a grade either. If you have this technology then your resume can be very foreign flavor written on ~
In fact, the template engine has a lot of nvelocity, Razor, stringtemplate and so on, Nvelocity is just one of them, from velocity. These template engines are similar in use, the general net programmers just touch the template engine is it, if used in Java will find that this thing and El expression in a sort of likeness.
OK, I will not continue to talk about, resources and notes I will stick to the following, we are on the right track ~
1. Create a new Web site to add a reference to NVelocity.dll
: Http://pan.baidu.com/s/1qWwkHzu
2. Create a template page, my such as: (suffix name casually, suggest first change to HTML, write and then change to your suffix)
Let's look at Nvelocity's grammar first.
————————————————— My Templates page ———————————————
Let's start with a simple analysis:
$title relative to the declaration of a variable called title, we will give him a value in the background, for example, like C # inside the string title, the background of an assignment can be displayed, and the server control lable a little resemblance (the nature is different)
#if ($p. Age <) #else #end; This is a syntax format, as opposed to if else, except for an end "note: A space greater than, less than, the middle of the sign. 】
$p. Does age know what it means? This is the power of nvelocity, where the $p represents a class, C # Object-oriented classes have field properties, the same applies here
—————————————————————————————————————— <! doctype html>
————————————————————————————————————————
3. Create a new generic handler
The code is affixed:
(Note what all help you to play, if you feel still do not understand directly to see the 5th step, I will teach you to use a word to fix him) <%@ webhandler language= "C #" class= " Onenvelocity " %> using System; using System.Web; using NVelocity; using nvelocity.app; using nvelocity.runtime; public class onenvelocity : ihttphandler{ public void processrequest (HttpContext context ) { //This type remember to change context. response.contenttype = "Text/html"; //. Create a template engine // 1. Initializing the template engine velocityEngine vengine = new velocityengine (); // 2. Setting the initialization properties of the template engine ~//setting the template the root path of the default template: vengine.setproperty (Runtimeconstants.file_resource_loader_path, context. Request.mappath ("~/nvelocity/templates/")); // 3. Initializing template template engine vengine.init (); //. Set the data used in the template // adds the data that needs to be used in the template to the Velocitycontext context object. // 1. Create a Velocitycontext context pairElephant Velocitycontext vcontext = new velocitycontext (); // 2. Setting data to a context object vcontext.put ("title", " This is my first template Engine page "); person p = new person () { Name = "A", age = 16, Email = "[email protected]", gender = "Women" }; vcontext.put ("P", p); // Vcontext.put ("Page_content", p); //Three, Merge template engine to load template files with Velocitycontext objects // when the merge is complete, the template engine will integrate the templates with the data and generate new content using (system.io.stringwriter strwriter = new system.io.stringwriter ()) { vengine.mergetemplate ("templater_index.dnt", "Utf-8", vcontext, strwriter); //Four. Respond to the new generated string to the user context. Response.Write (Strwriter); } } public bool isreusable { get { return false; } } } 4. Browse onenvelocity.ashx See Effect ~ (Not browse template page "template function is only the matrix") ———————————————————————————————————————————— 5. If you wearies or don't understand it, see this step we write so much code every time we render a page is it not very painful? Do not know what you think, inverse day from second day math teacher that learned a good habit ~ that is lazy, can simply make so annoying not brain pumping? So it encapsulates a nvelocityhelper, you can simply pass a few parameters to realize the effect of this template engine.
Let's say the following parameters: the absolute path of the TemplatePath template folder; Set the root path of the default template context HttpContext, general handler for the Context Object
FileName template file name + suffix encoding encoded format eg:utf-8
Dntdic Dictionary, put the page to replace the $ variable when the key
So how do you write it? See:
Three steps to write:
1. As above, the contenttype of the general processing procedure must first be changed to Text/htmlcontext. Response.ContentType = "text/html"; It means that the content you send is parsed in HTML format.
2. Create a value pair and assign it to him
dictionary<string, object> dic = new dictionary<string, object> ();
Dic. Add ("title", "This is my first template Engine page"); Person p = new person () {Name = "inverse Day", age =, email = "[email protected]", Gender = "Confidential"}; Dic. ADD ("P", p);
3. Call the nvelocity template engine dntnvelocityhelper.nvelocitytemplate (context. Request.mappath ("~/nvelocity/templates/"), Context, "templater_index.dnt", "Utf-8", DIC); It's done.
—————————————————————————————————————————— If you're curious about how I write, you can download the source code below, and I'll simply post it here using nvelocity ; using nvelocity.app; using nvelocity.runtime; using system; using System.collections.generic; using system.linq; using system.web; /// <summary Summary description of >/// MyNvelocity /// </summary>public static class dntnvelocityhelper{ /// <summary> /// anti-day oneself in order to lazy and write Nvelocityhelper class, note what I have written all, if there is a bug also look Haihan ~ /// </summary> /// <param name= " TemplatePath the absolute path to the > template folder; Set the root path where the default template is located </param> /// <param name= "Context" >HttpContext</param> /// <param name= "FileName "> template filename + suffix </param> /// <param name=" Encoding "> Encoding format eg:utf-8</param> /// < Param name= "Dntdic" >dictionary, put the page to replace the $ variable when the key </param> public static void nvelocitytemplate (string templatepath, Httpcontext context, string filename, string encoding, dictionary<string, object> dntdic) { //One, Create a template engine // 1. Initializing the template engine velocityengine vengine = new velocityengine (); // 2. Setting the template engine's initialInitialize Properties ~ Set the root path of the default template vengine.setproperty (Runtimeconstants.file_resource_loader_path, templatepath); // 3. Initializing template template engine vengine.init (); //. Set the data used in the template // adds the data that needs to be used in the template to the Velocitycontext context object. // 1. Create a Velocitycontext context object velocitycontext vcontext = new velocitycontext (); // 2. Setting data to a context object foreach (KeyValuePair <string, object> item in dntdic) { vcontext.put (item. Key, item. Value); } vcontext.put ("Page_content", p); //. Merge template engine to load template files with Velocitycontext objects // after the merger is complete, Within the template engine, templates and data are consolidated, and new content is generated using (system.io.stringwriter strwriter = new System.IO.StringWriter ()) { vengine.mergetemplate (Filename, encoding, vcontext, strwriter); //Four. Respond to the new generated string to the user Context. Response.Write (Strwriter); } }} ————————————————————————————————————————— - Data Collection:http://pan.baidu.com/s/1dditz0l
Reference Source: Http://pan.baidu.com/s/1dDB0pJb Nvelocity.dll:http://pan.baidu.com/s/1qwwkhzu official website:/http www.castleproject.org/download/
Just one word. nvelocity template engine, source + parsing + documentation + Data + annotations