public void TestJuxtaposePage() { SiteInfo site = new SiteInfo(); site.Copyright = "©2014 - 2015"; site.Description = ""; site.Host = "localhost"; site.KeyWords = ""; site.Logo = ""; site.Name = "xxx"; site.SiteDirectory = ""; site.Theme = "Blue"; site.ThemeDirectory = "theme"; site.Title = "jntemplate测试页"; site.Url = string.Concat("http://localhost"); if (!string.IsNullOrEmpty(site.SiteDirectory) && site.SiteDirectory != "/") { site.Url += "/" + site.SiteDirectory; } site.ThemeUrl = string.Concat(site.Url, "/", site.ThemeDirectory, "/", site.Theme); string basePath = new System.IO.DirectoryInfo(System.Environment.CurrentDirectory).Parent.Parent.FullName; string path = basePath + "\\templets\\nv"; NVelocity.Context.IContext ctx = new NVelocity.VelocityContext(); ctx.Put("func", new TemplateMethod()); ctx.Put("Site", site); NVelocity.App.VelocityEngine velocity = new NVelocity.App.VelocityEngine(); Commons.Collections.ExtendedProperties props = new Commons.Collections.ExtendedProperties(); props.AddProperty(NVelocity.Runtime.RuntimeConstants.RESOURCE_LOADER, "file"); props.AddProperty(NVelocity.Runtime.RuntimeConstants.FILE_RESOURCE_LOADER_PATH, path); props.AddProperty(NVelocity.Runtime.RuntimeConstants.INPUT_ENCODING, "utf-8"); props.AddProperty(NVelocity.Runtime.RuntimeConstants.OUTPUT_ENCODING, "utf-8"); velocity.Init(props); NVelocity.Template t = velocity.GetTemplate("questionlist.html"); string result; using (System.IO.StringWriter write = new StringWriter()) { t.Merge(ctx, write); result = write.ToString(); } //可直接查看项目录下的html/nv.html 文件效果 System.IO.File.WriteAllText(basePath + "\\html\\nv.html", result); }
/// <summary> /// ajax method for demo of parsing NVelocity Temp with VelocityEngine /// </summary> /// <param name="template"></param> /// <param name="dirctionary"></param> public void getParseResult(string template,string dirctionary) { if ((template == null) || (dirctionary == null)) { CancelView(); return; } var map=Newtonsoft.Json.JavaScriptConvert.DeserializeObject<Hashtable>(dirctionary); var writer = new System.IO.StringWriter(); var nve = new NVelocity.App.VelocityEngine(); nve.Init(); var context = new NVelocity.VelocityContext(map); nve.Evaluate(context, writer, "", template); RenderText(writer.GetStringBuilder().ToString()); writer.Close(); CancelView(); }
public static bool RenderStringTemplateToString(string templateString, Map dati, out string ret) { NVelocity.App.VelocityEngine engine = new NVelocity.App.VelocityEngine(); engine.Init(); NVelocity.VelocityContext vc = new NVelocity.VelocityContext(dati); StringBuilder sb = new StringBuilder(); string logMsg = null; bool bOk = false; try { using (StringWriter wr = new StringWriter(sb)) using (StringReader rdr = new StringReader(templateString)) bOk = engine.Evaluate(vc, wr, logMsg, rdr); ret = bOk ? sb.ToString() : logMsg; } catch (Exception e) { Logger.Warn("Errore in RenderStringTemplateToString", e); ret = e.Message; } return bOk; }
public static void RenderTemplateToWriter(string templateFile, TextWriter writer, NVelocity.VelocityContext vc) { NVelocity.App.VelocityEngine engine = null; IPersonalSettings provider = PersonalSettings.Provider; if (provider == null) { Commons.Collections.ExtendedProperties props = new Commons.Collections.ExtendedProperties(); props.SetProperty(NVelocity.Runtime.RuntimeConstants.RESOURCE_LOADER, "file"); props.SetProperty(NVelocity.Runtime.RuntimeConstants.FILE_RESOURCE_LOADER_PATH, _defaultPath); props.SetProperty(NVelocity.Runtime.RuntimeConstants.FILE_RESOURCE_LOADER_CACHE, "true"); engine = new NVelocity.App.VelocityEngine(props); } else engine = (NVelocity.App.VelocityEngine)PersonalSettings.Provider.ViewEngine; NVelocity.Template template = engine.GetTemplate(templateFile); template.Merge(vc, writer); }
public void TestJuxtaposePage() { SiteInfo site = new SiteInfo(); site.Copyright = "©2014 - 2015"; site.Description = ""; site.Host = "localhost"; site.KeyWords = ""; site.Logo = ""; site.Name = "xxx"; site.SiteDirectory = ""; site.Theme = "Blue"; site.ThemeDirectory = "theme"; site.Title = "jntemplate测试页"; site.Url = string.Concat("http://localhost"); if (!string.IsNullOrEmpty(site.SiteDirectory) && site.SiteDirectory != "/") { site.Url += "/" + site.SiteDirectory; } site.ThemeUrl = string.Concat(site.Url, "/", site.ThemeDirectory, "/", site.Theme); string basePath = new System.IO.DirectoryInfo(System.Environment.CurrentDirectory).Parent.Parent.FullName; string path = basePath + "\\templets\\nv"; NVelocity.Context.IContext ctx = new NVelocity.VelocityContext(); ctx.Put("func", new TemplateMethod()); ctx.Put("Site", site); NVelocity.App.VelocityEngine velocity = new NVelocity.App.VelocityEngine(); Commons.Collections.ExtendedProperties props = new Commons.Collections.ExtendedProperties(); props.AddProperty(NVelocity.Runtime.RuntimeConstants.RESOURCE_LOADER, "file"); props.AddProperty(NVelocity.Runtime.RuntimeConstants.FILE_RESOURCE_LOADER_PATH, path); props.AddProperty(NVelocity.Runtime.RuntimeConstants.INPUT_ENCODING, "utf-8"); props.AddProperty(NVelocity.Runtime.RuntimeConstants.OUTPUT_ENCODING, "utf-8"); velocity.Init(props); NVelocity.Template t = velocity.GetTemplate("questionlist.html"); string result; using (System.IO.StringWriter write = new StringWriter()) { t.Merge(ctx, write); result = write.ToString(); } //可直接查看项目录下的html/nv.html 文件效果 System.IO.File.WriteAllText(basePath + "\\html\\nv.html", result); }