封装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]

using System;
using System.Data;

using AspNet.App.UI;

namespace AspNet.Web.User
{
 public abstract class UserMasterHandler : BaseHandler
 {
 public override void Handler_Load(object sender, EventArgs e)
 {
 //这里的数据主要是为了呈现Master配置文件的
 DataTable dt = new DataTable();
 dt.Columns.Add("id");
 dt.Columns.Add("name");

 for (int i = 1; i <= 10; i )
 {
 DataRow dr = dt.NewRow();
 dr["id"] = i.ToString("D2");
 dr["name"] = "测试名字" i.ToString("D2");

 dt.Rows.Add(dr);
 }

 this.TemplateData.Add("dt", dt);
 this.TemplateData.Add("title", get_Title());
 this.TemplateData.Add("centerHtml", get_CenterHtml());

 this.TemplateFile = ViewPath("/user/userMaster.htm");
 }

 public abstract string get_Title();//为子文件必须实现的方法,子文件可以不关心Master文件中到底有什么标记或有多少标记,只需要实现了Master的所有abstract方法即可
 public abstract string get_CenterHtml();
 }
}

转载于:https://my.oschina.net/haojielyb/blog/4266

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值