Velocity使用方法简介
时间:2010-02-04
来源:互联网
以前用winforms写模板生成,现在没必要了,有了Velocity(以下简称V),就可以写出web的模板生成了。 使用很简单,下载了之后,引用dll,我这里写了一个通用的类 using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Collections; using NVelocity; using NVelocity.App; using NVelocity; using NVelocity.Runtime; using Commons.Collections; using System.IO; using NVelocity.Context; /// <summary> /// BaseVelocity 的摘要说明 /// </summary> public class BaseVelocity { /// <summary> /// Common /// </summary> /// <param name="tempAbsPath">模板路径</param> /// <param name="outAbsPath">输出绝对路径</param> /// <param name="parameters">初始参数</param> /// <param name="tempEncoding">模板编码[options]</param> /// <param name="outEncoding">输出编码[optioins]</param> /// <param name="returnStr"></param> /// <returns></returns> private string create(string tempAbsPath, string outAbsPath,Hashtable parameters,string tempEncoding, string outEncoding,bool returnStr) { if (tempAbsPath == "") { throw new ArgumentException("模板参数错误"); } if (outAbsPath == "") { throw new ArgumentException("输出参数错误"); } if (tempEncoding == "") tempEncoding = "utf-8"; if (outEncoding == "") outEncoding = "utf-8"; //创建NVelocity引擎的实例对象 VelocityEngine velocity = new VelocityEngine(); //初始化该实例对象 string root = tempAbsPath.Substring(0, tempAbsPath.LastIndexOf("\\") + 1); string fileName = tempAbsPath.Substring(tempAbsPath.LastIndexOf("\\") + 1); ExtendedProperties props = new ExtendedProperties(); props.AddProperty(RuntimeConstants.RESOURCE_LOADER, "file"); props.AddProperty(RuntimeConstants.INPUT_ENCODING, tempEncoding); props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, root); props.AddProperty(RuntimeConstants.OUTPUT_ENCODING, outEncoding); velocity.Init(props); //从文件中读取模板 NVelocity.Template temp = velocity.GetTemplate(fileName); IContext context = new VelocityContext(); if (parameters != null) { foreach (string key in parameters.Keys) { context.Put(key, parameters[key]); } } //合并模板 StringWriter writer = new StringWriter(); //velocity.MergeTemplate(context, writer); temp.Merge(context, writer); //生成静态页 if (!returnStr) { using (StreamWriter writer2 = new StreamWriter(outAbsPath, false, System.Text.Encoding.GetEncoding(outEncoding), 200)) { writer2.Write(writer); writer2.Flush(); writer2.Close(); } } else return writer.ToString(); return null; } /// <summary> /// 创建静态文件 /// </summary> /// <param name="tempAbsPath">模板绝对路径</param> /// <param name="outAbsPath">输出绝对路径</param> /// <param name="parameters">初始参数</param> /// <param name="tempEncoding">模板编码[options]</param> /// <param name="outEncoding">输出编码[optioins]</param> /// <param name="returnStr"></param> /// <returns></returns> public void creatFile(string tempAbsPath, string outAbsPath, Hashtable parameters, string tempEncoding, string outEncoding) { create(tempAbsPath,outAbsPath,parameters,tempEncoding,outEncoding,false); } /// <summary> /// 创建输出字符串 /// </summary> /// <param name="tempAbsPath">模板绝对路径</param> /// <param name="outAbsPath">输出绝对路径</param> /// <param name="parameters">初始参数</param> /// <param name="tempEncoding">模板编码[options]</param> /// <param name="outEncoding">输出编码[optioins]</param> /// <param name="returnStr"></param> /// <returns></returns> public string createStr(string tempAbsPath, string outAbsPath, Hashtable parameters, string tempEncoding, string outEncoding) { return create(tempAbsPath, outAbsPath, parameters, tempEncoding, outEncoding, true); } } 这种是通过文件加载模板的 以下可以通过变量加载模板 关键代码: System.Text.StringBuilder builder = new System.Text.StringBuilder(); builder.Append("#foreach($u in $ListUsers)"r"n" + "#beforeall"r"n" + "<table border=""0"" cellpadding=""10"" cellspacing=""10"">" + "<tr><td>Name</td><td>Sex</td><td>City</td></tr>" + "#each"r"n" + "<tr>" + "<td>$u.Name</td>" + "<td>$u.Sex</td>" + "<td>$u.City</td>" + "</tr>" + "#afterall"r"n" + "</table>" + "#nodata"r"n" + "暂无用户资料"r"n" + "#end"); VelocityEngine vltEngine = new VelocityEngine(); vltEngine.Init(); VelocityContext vltContext = new VelocityContext(); vltContext.Put("PageTitle", "字符串模板例子"); vltContext.Put("ListUsers", listUsers); System.IO.StringWriter vltWriter = new System.IO.StringWriter(); vltEngine.Evaluate(vltContext, vltWriter, null, builder.ToString()); Response.Write(vltWriter.GetStringBuilder().ToString()); |
作者: swlyb 发布时间: 2010-02-04
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28