using System;
using System.Web;
using System.Collections.Generic;
using System.Text;
using System.IO;
using NVelocity;
using NVelocity.App;
using NVelocity.Context;
using NVelocity.App.Tools;
using NVelocity.Runtime;
using Commons.Collections;
namespace Web
{
/// <summary>
/// NVelocity模板工具类 VelocityHelper
/// </summary>
public class VelocityHelper
{
private VelocityEngine velocity = null;
private IContext context = null;
public VelocityHelper()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 构造函数
/// </summary>
public VelocityHelper(string templatePath)
{
Init(templatePath);
}
/// <summary>
/// 初始话NVelocity模块
/// </summary>
/// <param name="templatDir">模板文件夹路径</param>
public void Init(string templatDir)
{
//创建VelocityEngine实例对象
velocity = new VelocityEngine();
//使用设置初始化VelocityEngine
ExtendedProperties props = new ExtendedProperties();
props.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file");
props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, HttpContext.Current.Server.MapPath(templatDir));
props.AddProperty(RuntimeConstants.INPUT_ENCODING, "utf-8");
props.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "utf-8");
velocity.Init(props);
//为模板变量赋值
context = new VelocityContext();
context.Put("formatter", new VelocityFormatter(context));
}
/// <summary>
/// 给模板变量赋值
/// </summary>
/// <param name="key">模板变量</param>
/// <param name="value">模板变量值</param>
public void PutSet(string key, object value)
{
if (context == null)
{
context = new VelocityContext();
}
context.Put(key, value);
}
/// <summary>
/// 显示模板
/// </summary>
/// <param name="templatFileName">模板文件名</param>
public void Display(string templatFileName)
{
//从文件中读取模板
//Template template = velocity.GetTemplate(templatFileName);
Template template = velocity.GetTemplate(templatFileName, "UTF-8");
//合并模板
StringWriter writer = new StringWriter();
template.Merge(context, writer);
//输出
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Write(writer.ToString());
//HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
#region 使用方法:
/*
VelocityHelper vh = new VelocityHelper();
vh.Init(@"templates");//指定模板文件的相对路径
vh.PutSet("title", "员工信息");
vh.PutSet("comName","成都xxxx里公司");
vh.PutSet("property”,"天营");
ArrayList aems = new ArrayList();
//使用tp1.htm模板显示
vh.Display("tp1.htm");
*/
#endregion
}
}