NVelocity 是 Velocity 的 .NET 实现,该项目的目的是将 Jakarta Velocity 项目移植到 C# 平台。它允许任何人仅仅简单的使用模板语言来引用由 .NET 代码定义的对象。从而使得界面设计人员与 .NET 程序开发人员基本分离。
项目地址:http://nvelocity.sourceforge.net/
NVelocity 模版引擎助手类实现如下:
using System.IO;
using System.Web;
using Commons.Collections;
using NVelocity;
using NVelocity.App;
using NVelocity.Context;
using NVelocity.Runtime;
/// <summary>
/// NVelocity 模版引擎助手类。
/// </summary>
public class NVelocityHelper
{
private VelocityEngine velocity = null;
private IContext context = null;
/// <summary>
/// 初始化 <see cref="NVelocityHelper"/> 类的新实例。
/// </summary>
public NVelocityHelper() { }
/// <summary>
/// 使用指定的模版文件初始化 <see cref="NVelocityHelper"/> 类的新实例。
/// </summary>
/// <param name="templateDir">模板文件夹路径。</param>
public NVelocityHelper(string templateDir)
{
Init(templateDir);
}
/// <summary>
/// 初始化 NVelocity 模块。
/// </summary>
/// <param name="templateDir">模版文件所在的文件夹。</param>
public void Init(string templateDir)
{
// 创建 VelocityEngine 实例对象
velocity = new VelocityEngine();
// 使用设置初始化 VelocityEngine
ExtendedProperties props = new ExtendedProperties();
props.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file");
props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, templateDir);
props.AddProperty(RuntimeConstants.INPUT_ENCODING, "utf-8");
props.AddProperty(RuntimeConstants.OUTPUT_ENCODING, "utf-8");
velocity.Init(props);
// 为模板变量赋值
context = new VelocityContext();
}
/// <summary>
/// 给模板变量赋值
/// </summary>
/// <param name="key">模板变量键。</param>
/// <param name="value">模板变量值。</param>
public void Put(string key, object value)
{
if (context == null)
{
context = new VelocityContext();
context.Put(key, value);
}
else
{
context.Put(key, value);
}
}
/// <summary>
/// 获取或设置 <see cref="IContext"/> 对象。
/// </summary>
public IContext Context
{
set { context = value; }
get { return context; }
}
/// <summary>
/// 获取模版文件的字符串。
/// </summary>
/// <param name="templateFileName">模版文件名称。</param>
/// <returns>模版文件的字符串。</returns>
public StringWriter GetResultString(string templateFileName)
{
// 从文件中读取模板
Template template = velocity.GetTemplate(templateFileName);
// 合并模板
StringWriter writer = new StringWriter();
template.Merge(context, writer);
return writer;
}
/// <summary>
/// 显示结果内容。
/// </summary>
/// <param name="templatFileName">模版文件名称。</param>
public void Display(string templateFileName)
{
StringWriter writer = GetResultString(templateFileName);
//输出
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Write(writer.ToString());
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
/// <summary>
/// 生成 HTML 文件, 若生成成功返回 true,否则返回 false。
/// </summary>
/// <param name="templateFileName">模版文件名。</param>
/// <param name="targetFolder">目标文件夹。</param>
/// <param name="fileName">生成的文件名。</param>
/// <returns>若生成成功返回 true,否则返回 false。</returns>
public bool GenerateShtml(string templateFileName, string targetFolder, string fileName)
{
StringWriter writer = GetResultString(templateFileName);
try
{
if (Directory.Exists(targetFolder))
{
File.WriteAllText(targetFolder + "\\" + fileName, writer.ToString(), new System.Text.UTF8Encoding(true));
return true;
}
else
{
return false;
}
}
catch
{
return false;
}
}
}
NVelocity 的更多使用方法请参考官方文档。