封装ASP.NET模板引擎NVelocity应用实例(转)

最近关注一下asp.net的模板引擎,在网上看介绍有不少的模板引擎,其中一个是NVelocity模板引擎,挺不错的,也测试过一下,于是在网上有人做二次封装来调用。来看下吧。

先看代码:

BaseHandler.cs
Code [http://www.xueit.com]
using System;
using System.IO;
using System.Web;
using System.Text;
using System.Collections;

using NVelocity;
using NVelocity.App;
using NVelocity.Context;
using NVelocity.Runtime;
using Commons.Collections;

namespace AspNet.App.UI
{
public abstract class BaseHandler : IHttpHandler
{
private static readonly VelocityEngine viewEngine = new VelocityEngine();

static BaseHandler()
{
//TODO:这里的硬编码可以改成配置文件的方式
ExtendedProperties extend = new ExtendedProperties();
extend.AddProperty(RuntimeConstants.COUNTER_NAME, "i");
extend.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file");
extend.AddProperty(RuntimeConstants.INPUT_ENCODING, "utf-8");
extend.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "utf-8");

string appPath = AppDomain.CurrentDomain.BaseDirectory;
extend.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, appPath);

//模板的缓存设置
extend.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_CACHE, true); //是否缓存
extend.AddProperty("file.resource.loader.modificationCheckInterval", (Int64)30); //缓存时间(秒)
viewEngine.Init(extend);
}

public HttpContext HttpContext
{
get;
private set;
}

public HttpRequest Request
{
get;
private set;
}

public HttpResponse Response
{
get;
private set;
}

private Hashtable templateData = new Hashtable();
public Hashtable TemplateData
{
get { return templateData; }
}

private string templateFile = string.Empty;
public string TemplateFile
{
get { return templateFile; }
set { templateFile = value; }
}

public void ProcessRequest(HttpContext context)
{
this.HttpContext = context;
this.Request = context.Request;
this.Response = context.Response;

//此方法主要进行数据的获取
Handler_Load(this, EventArgs.Empty);

//输出
IContext ctx = new VelocityContext(templateData);
viewEngine.MergeTemplate(TemplateFile, "utf-8", ctx, context.Response.Output);
}

//局部文件的呈现
protected string Merge(Hashtable data, string fileName)
{
IContext ctx = new VelocityContext(data);
StringBuilder sb = new StringBuilder(512);
StringWriter writer = new StringWriter(sb);

viewEngine.MergeTemplate(fileName, "utf-8", ctx, writer);
return sb.ToString();
}

public abstract void Handler_Load(object sender, EventArgs e);

public bool IsReusable
{
get { return false; }
}
}
}

下面给出具体的应用
为实现Master效果
对上面的代码进行2次封装:

2次封装:
Code [http://www.xueit.com]

  1. using System;   
  2. using System.Data;   
  3.   
  4. using AspNet.App.UI;   
  5.   
  6. namespace AspNet.Web.User   
  7. {   
  8.  public abstract class UserMasterHandler : BaseHandler   
  9.  {   
  10.  public override void Handler_Load(object sender, EventArgs e)   
  11.  {   
  12.  //这里的数据主要是为了呈现Master配置文件的   
  13.  DataTable dt = new DataTable();   
  14.  dt.Columns.Add("id");   
  15.  dt.Columns.Add("name");   
  16.   
  17.  for (int i = 1; i <= 10; i )   
  18.  {   
  19.  DataRow dr = dt.NewRow();   
  20.  dr["id"] = i.ToString("D2");   
  21.  dr["name"] = "测试名字" i.ToString("D2");   
  22.   
  23.  dt.Rows.Add(dr);   
  24.  }   
  25.   
  26.  this.TemplateData.Add("dt", dt);   
  27.  this.TemplateData.Add("title", get_Title());   
  28.  this.TemplateData.Add("centerHtml", get_CenterHtml());   
  29.   
  30.  this.TemplateFile = ViewPath("/user/userMaster.htm");   
  31.  }   
  32.   
  33.  public abstract string get_Title();//为子文件必须实现的方法,子文件可以不关心Master文件中到底有什么标记或有多少标记,只需要实现了Master的所有abstract方法即可   
  34.  public abstract string get_CenterHtml();   
  35.  }   
posted @ 2010-09-09 10:28  bsso  阅读(596)  评论(0)    收藏  举报
土蜀佃博客