public static string FormatText(string templateText, VelocityContext context) { if (!initialized) { var properties = new Commons.Collections.ExtendedProperties(); properties.AddProperty("resource.loader", "custom"); properties.AddProperty("custom.resource.loader.class", "ASC.Common.Utils.TextLoader; ASC.Common"); properties.AddProperty("input.encoding", Encoding.UTF8.WebName); properties.AddProperty("output.encoding", Encoding.UTF8.WebName); Velocity.Init(properties); initialized = true; } using (var writer = new StringWriter()) { Template template; var key = templateText.GetHashCode().ToString(); if (!patterns.TryGetValue(key, out template)) { template = Velocity.GetTemplate(templateText); patterns.TryAdd(key, template); } template.Merge(context, writer); return(writer.GetStringBuilder().ToString()); } }
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); }
public static string FormatText(string templateText, VelocityContext context) { if (!_isVelocityInitialized) { var properties = new Commons.Collections.ExtendedProperties(); properties.AddProperty("resource.loader", "custom"); properties.AddProperty("custom.resource.loader.class", "ASC.Common.Utils.TextLoader; ASC.Common"); properties.AddProperty("input.encoding", Encoding.UTF8.WebName); properties.AddProperty("output.encoding", Encoding.UTF8.WebName); Velocity.Init(properties); _isVelocityInitialized = true; } using (var writer = new StringWriter()) { var template = Patterns.Get(templateText.GetHashCode().ToString(), () => Velocity.GetTemplate(templateText)); template.Merge(context, writer); return writer.GetStringBuilder().ToString(); } }
static HtmlGenerator() { velocity = new VelocityEngine(); var p = new Commons.Collections.ExtendedProperties(); #if DEBUG string temp = Application.ExecutablePath; string[] split = Application.ExecutablePath.Split('\\'); string pathRoot = string.Join("\\", split, 0, split.Length - 3); //cssFile = string.Format(@"file:///{0}\html\best.css", pathRoot); //scriptFile = string.Format(@"file:///{0}\html\scripts.js", pathRoot); p.AddProperty("file.resource.loader.path", new System.Collections.ArrayList(new string[] { pathRoot })); #endif velocity.Init(p); baseContext = new VelocityContext(); baseContext.Put("script", "scripts.js"); baseContext.Put("css", "best.css"); }