NVelocity template engine

Original: NVelocity template engine

Brief introduction

      NVelocity .NET is a template engine (template engine) based. It allows anyone to use just a simple template language (template language) to reference objects defined by the .NET code.

grammar

   
#include ( "head.htm") // import documents 
#parse ( "head.htm") // import documents, and include the difference is, parse can be resolved in the object file 
#set ($ age = 20) // the definition of a variable age, and through the set assignment     
$ age // show variable age
 
 
     // simple judgment               
      #if ($ age> 10)      
         ignorant of Sao 
      #else 
         the Mood for Love 
      #end 
     // complex judgment 
     #if ($ age> 5 && $ age <= 10) 
          to learn English, so easy! 
      #elseif ($ age> 10 && $ age <= 16) 
          job, how do 
      #elseif ($ age> 16 && $ age <= 19) 
          Chemistry difficult! 
      #else 
          Then we began to miss school! 
      #end 
      // foreach loop iterates through the collection element 
      # Foreach ($ in the p-$ Model.Persons)    
         $ p.Name // Name 
         $ p.Age // Age 
         $ p.Phone // phone number 
      #end
    

NVelocity with general processing procedure (simple use)

      
 <the Table class = "DOC-the Table"> 
          <thead> <TR> <td> Name </ td> <td> Age </ td> <td> phone number </ td> </ TR> </ thead> 
          < tbody> 
             #foreach (in $ P $ Model.Persons) // cycle tr generated by the content Model.Persons $ 
                <tr> <TD> $ p.Name </ TD> <TD> $ p.Age </ TD> <TD> $ p.Phone </ TD> 
             #end 
          </ tbody> 
</ Table>
    the Test class public: the IHttpHandler { 
        public void the ProcessRequest (the HttpContext context) { 
            context.Response.ContentType = "text / HTML"; // set the output HTML 
            List <Person> = new new List List <Person> (); // cycle generating functional type List <Person> 
            for (int I =. 1; I <= 10; I ++) { 
                the Person P = new new the Person () {the Name = "Yangtze" + i + "No", Age = 18 + i, Phone = (18801001618 I +) .ToString ()}; 
                list.add (P); 
            } 
            var Data} = {Persons = new new list; // bind Persons attribute list to anonymous class 

            VelocityEngine vltEngine = new VelocityEngine (); // initialize NVeloity 
            vltEngine.SetProperty (RuntimeConstants.RESOURCE_LOADER, "file");

            // Use HostingEnvironment.MapPath file specified template file in the folder where the namespace System.Web.Hosting 
            vltEngine.SetProperty (RuntimeConstants.FILE_RESOURCE_LOADER_PATH, HostingEnvironment.MapPath ( "~ / Templates")); 

            vltEngine.Init (); 

            VelocityContext vltContext = new VelocityContext (); // NVelocity context 

            // specified data to the data key as a "Model", page template (view) $ Model, by the method put into NVelocityContext in 
            vltContext.Put ( "Model", data) ;   

            template vltTemplate = vltEngine.GetTemplate ( "the Test.htm"); // get the path and file name where the template (template relative to the specified folder location) 
            StringWriter vltWriter = new new StringWriter ();  
            vltTemplate.Merge (vltContext, vltWriter ); // Render resolved through internal traversing the "Model"
            String HTM = vltWriter.GetStringBuilder ( ) .ToString (); 

            context.Response.Write (HTM);
        }        
    }
    
    //联系人
    public class Person {
        public string Name { get; set; }
        public int Age { get; set; }
        public string Phone { get; set; }
    }

to sum up

     1. allows us to page designers can focus only by NVelocity display the page, but allows us to .NET application developers focus on business logic implementation, to post-maintenance also provides great convenience;

     2.NVelocity and general processing program combined to avoid complex aspx page life cycle;

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/11058092.html
Recommended