zoukankan      html  css  js  c++  java
  • 初见NVelocity模板引擎

                //using NVelocity.App;
                //using NVelocity;
                //using NVelocity.Runtime;
                VelocityEngine vltEngine = new VelocityEngine();//创建一个NVelocity对象
                vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file");//设置属性:本地文件资源来源为file系统
                vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath("~/template"));//设置属性:其文件路径,为所有模板根目录
                vltEngine.Init();
                VelocityContext vltContext = new VelocityContext();//创建一个NVelocity的上下文对象
                vltContext.Put("errorMsg", "");//模板中站位的替换,其中key为string,value为obj。因此更适合于替换
                Template vltTemplate = vltEngine.GetTemplate("template.html");//指定获取模板的文件名
                System.IO.StringWriter vltWriter = new System.IO.StringWriter();
                vltTemplate.Merge(vltContext, vltWriter);
                string html = vltWriter.GetStringBuilder().ToString();
                context.Response.Write(html);//最终响应

    NVelocity是实用但比较土的模板替换引擎,不过已经可以适用于大多数项目的开发。以上是其常用的代码,要用直接复制黏贴即可。

    按其规定,在html代码中的key应该以$开头。

    强大之处:

    1.可以直接传入一个对象,而在模板中再对对象的属性进行访问。

    vltContext.Put("data", 对象);

    在html中通过 $data.属性名 来获取对象属性。

    2可以在模板中做一些简单的逻辑处理。

    如:

    条件分支:

    #if(条件)

    html语句

    #else

    html语句

    #end

    循环:

    #foreach($变量 in $data)

    html代码

    #end

    封装代码:

     1 using NVelocity;
     2 using NVelocity.App;
     3 using NVelocity.Runtime;
     4 using System;
     5 using System.Collections.Generic;
     6 using System.Linq;
     7 using System.Web;
     8 
     9 namespace Web
    10 {
    11     public class RenderHtml
    12     {
    13         public static string Render(string fileName, object data)
    14         {
    15             VelocityEngine vltEngine = new VelocityEngine();
    16             vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file");
    17             vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath("~/templates"));
    18             vltEngine.Init();
    19             VelocityContext vltContext = new VelocityContext();
    20             vltContext.Put("data", data);
    21             Template vltTemplate = vltEngine.GetTemplate(fileName);
    22             System.IO.StringWriter vltWriter = new System.IO.StringWriter();
    23             vltTemplate.Merge(vltContext, vltWriter);
    24             string html = vltWriter.GetStringBuilder().ToString();
    25             return html;
    26         }
    27     }
    28 }
  • 相关阅读:
    C#磁吸屏幕窗体类库
    准备
    我写的诗
    How to turn off a laptop keyboard
    How to tell which commit a tag points to in Git?
    Why should I care about lightweight vs. annotated tags?
    How to get rid of “would clobber existing tag”
    Facebook, Google and Twitter threaten to leave Hong Kong over privacy law changes
    The need for legislative reform on secrecy orders
    Can a foreign key be NULL and/or duplicate?
  • 原文地址:https://www.cnblogs.com/jbdxbl/p/9366269.html
Copyright © 2011-2022 走看看