/// <summary>
/// 基于NVelocity的模板文件生成辅助类
/// </summary>
public class NVelocityHelper
{
protected VelocityContext context;
protected Template template;
protected string templateFile;
/// <summary>
/// 存放键值的字典内容
/// </summary>
private Dictionary<string, object> KeyObjDict = new Dictionary<string, object>();
/// <summary>
/// 添加一个键值对象
/// </summary>
/// <param name="key">键,不可重复</param>
/// <param name="value">值</param>
/// <returns></returns>
public NVelocityHelper AddKeyValue(string key, object value)
{
if (!KeyObjDict.ContainsKey(key))
{
KeyObjDict.Add(key, value);
}
return this;
}................
/// <summary>
/// 初始化模板引擎
/// </summary>
protected virtual void InitTemplateEngine()
{
try
{
//Velocity.Init(NVELOCITY_PROPERTY);
VelocityEngine templateEngine = new VelocityEngine();
templateEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file");
templateEngine.SetProperty(RuntimeConstants.INPUT_ENCODING, "utf-8");
templateEngine.SetProperty(RuntimeConstants.OUTPUT_ENCODING, "utf-8");
//如果设置了FILE_RESOURCE_LOADER_PATH属性,那么模板文件的基础路径就是基于这个设置的目录,否则默认当前运行目录
templateEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, AppDomain.CurrentDomain.BaseDirectory);
templateEngine.Init();
template = templateEngine.GetTemplate(templateFile);
}
catch (ResourceNotFoundException re)
{
string error = string.Format("Cannot find template " + templateFile);
LogTextHelper.Error(error);
throw new Exception(error, re);
}
catch (ParseErrorException pee)
{
string error = string.Format("Syntax error in template " + templateFile + ":" + pee.StackTrace);
LogTextHelper.Error(error);
throw new Exception(error, pee);
}
}
/// <summary>
/// 初始化上下文的内容
/// </summary>
private void InitContext()
{
context = new VelocityContext();
foreach (string key in KeyObjDict.Keys)
{
context.Put(key, KeyObjDict[key]);
}
}
/// <summary>
///根据模板创建输出的文件,并返回生成的文件路径
/// </summary>
public virtual string ExecuteFile()
{
string fileName = "";
if (template != null)
{
string filePath = CheckEndBySlash(directoryOfOutput);
fileName = filePath + fileNameOfOutput + fileExtension;
if (!string.IsNullOrEmpty(filePath) && !Directory.Exists(filePath))
{
Directory.CreateDirectory(filePath);
}
//LogTextHelper.Debug(string.Format("Class file output path:{0}", fileName));
InitContext();
using (StreamWriter writer = new StreamWriter(fileName, false))
{
template.Merge(context, writer);
}
}
return fileName;
}
/// <summary>
/// 根据模板输出字符串内容
/// </summary>
/// <param name="templateFile"></param>
/// <returns></returns>
public string ExecuteString()
{
InitContext();
System.IO.StringWriter writer = new System.IO.StringWriter();
template.Merge(context, writer);
return writer.GetStringBuilder().ToString();
}
/// <summary>
/// 合并字符串的内容
/// </summary>
/// <returns></returns>
public string ExecuteMergeString(string inputString)
{
VelocityEngine templateEngine = new VelocityEngine();
templateEngine.Init();
InitContext();
System.IO.StringWriter writer = new System.IO.StringWriter();
templateEngine.Evaluate(context, writer, "mystring", inputString);
return writer.GetStringBuilder().ToString();
}
private void btnGenerateFile_Click(object sender, EventArgs e)
{
string tempalte = "Template/template.htm";//相对目录
TestInfo info = new TestInfo();
info.Title = "测试标题";
info.Content = "测试内容,这是测试内容";
info.Datetime = DateTime.Now;
NVelocityHelper adapter = new NVelocityHelper(tempalte);
adapter.AddKeyValue("title", "This is a title")
.AddKeyValue("content", "This is a Content")
.AddKeyValue("datetime", System.DateTime.Now)
.AddKeyValue("TestInfo", info);
adapter.FileNameOfOutput = "testTemplate";
string filePath = adapter.ExecuteFile();
if (!string.IsNullOrEmpty(filePath))
{
Process.Start(filePath);
}
}
private void btnGenerate_Click(object sender, EventArgs e)
{
string tempalte = "Template/template.htm";//相对目录
TestInfo info = new TestInfo();
info.Title = "测试标题";
info.Content = "测试内容,这是测试内容";
info.Datetime = DateTime.Now;
NVelocityHelper adapter = new NVelocityHelper(tempalte);
adapter.AddKeyValue("title", "This is a title")
.AddKeyValue("content", "This is a Content")
.AddKeyValue("datetime", System.DateTime.Now)
.AddKeyValue("TestInfo", info);
this.txtCode.Text = adapter.ExecuteString();
}
private void btnMergeString_Click(object sender, EventArgs e)
{
System.Text.StringBuilder builder = new System.Text.StringBuilder();
builder.Append(
"${Title}\r\n" +
"$Content\r\n" +
"$Digest\r\n" +
"$Author\r\n" +
"$Keyword\r\n" +
"$DateTime\r\n");
NVelocityHelper adapter = new NVelocityHelper();
adapter.AddKeyValue("Title", "标题").
AddKeyValue("Content", "内容").
AddKeyValue("Digest", "摘要").
AddKeyValue("Author", "作者").
AddKeyValue("Keyword", "关键词").
AddKeyValue("DateTime", DateTime.Now);
this.txtCode.Text = adapter.ExecuteMergeString(builder.ToString());
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有