NVelocity.dll是Java中常用的一个模版,下面是常用的模版引擎
1,返回string类型的html代码
/// <summary> /// 获取html模版 /// </summary> /// <param name="templateName">渲染的html模版的名称</param> /// <param name="data">渲染html的数据</param> /// <returns>渲染生成的html代码string类型</returns> public static string RenderHtml(string templateName, object data) { string rtn = string.Empty; VelocityEngine vltEngine = new VelocityEngine(); vltEngine.SetProperty(RuntimeConstants.RESOURCE_LOADER, "file"); vltEngine.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, System.Web.Hosting.HostingEnvironment.MapPath("~/Templates"));//模板文件所在的文件夹 vltEngine.Init(); VelocityContext vltContext = new VelocityContext(); vltContext.Put("data", data);//设置参数,在模板中可以通过$data来引用,可以同时设定多个参数 Template vltTemplate = vltEngine.GetTemplate(templateName); System.IO.StringWriter vltWriter = new System.IO.StringWriter(); vltTemplate.Merge(vltContext, vltWriter); rtn = vltWriter.GetStringBuilder().ToString(); return rtn; }
Nvelocity