private MergeTemplate ( String templateName, IContext context, |
||
templateName | String | |
context | IContext | |
writer | ||
return | bool |
/// <summary> /// Merge the specified Velocity template with the given model and write /// the result to the given Writer. /// </summary> /// <param name="velocityEngine">VelocityEngine to work with</param> /// <param name="templateLocation">the location of template, relative to Velocity's resource loader path</param> /// <param name="encoding">encoding the encoding of the template file</param> /// <param name="model">the Hashtable that contains model names as keys and model objects</param> /// <param name="writer">writer the TextWriter to write the result to</param> /// <exception cref="VelocityException">thrown if any exception is thrown by the velocity engine</exception> public static void MergeTemplate( VelocityEngine velocityEngine, string templateLocation, string encoding, Hashtable model, TextWriter writer) { try { VelocityContext velocityContext = new VelocityContext(model); velocityEngine.MergeTemplate(templateLocation, encoding, velocityContext, writer); } catch (VelocityException) { throw; } catch (Exception ex) { throw new VelocityException(ex.ToString(), ex); } }
/// <summary> /// Creates a velocity engine that is initiated. It loads up /// templates from the 'Templates' folder. /// </summary> /// <returns></returns> protected virtual string CreateSqlOutput(IEnumerable<Migration> migrations) { VelocityEngine velocityEngine = new VelocityEngine(); ExtendedProperties extendedProperties = new ExtendedProperties(); extendedProperties.SetProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, AppDomain.CurrentDomain.BaseDirectory + "\\Templates"); velocityEngine.Init(extendedProperties); var context = new VelocityContext(); context.Put("migrations", migrations.OrderBy(migration => migration.MigrationDate)); var stringWriter = new StringWriter(); velocityEngine.MergeTemplate("deployment_tsql.vm", "ISO-8859-1", context, stringWriter); return stringWriter.GetStringBuilder().ToString(); }
/// <summary> /// 生成内容 /// </summary> /// <returns></returns> public string GetString() { VelocityEngine ve = new VelocityEngine();//模板引擎实例化 ExtendedProperties ep = new ExtendedProperties();//模板引擎参数实例化 ep.AddProperty("file.resource.loader.modificationCheckInterval", (Int64)300); //缓存时间(秒) ve.Init(ep); VelocityContext vc = new VelocityContext(); //当前的数据信息载体集合 foreach (var item in Items) { vc.Put(item.Key, item.Value); } TextWriter writer = new StringWriter(); ve.MergeTemplate(TemplateName, "utf-8", vc, writer); return writer.ToString(); }
public string RenderTemplate(string masterPage, string templateName, IDictionary<string, object> data) { if (string.IsNullOrEmpty(templateName)) { throw new ArgumentException("The \"templateName\" parameter must be specified", "templateName"); } var name = !string.IsNullOrEmpty(masterPage) ? masterPage : templateName; var engine = new VelocityEngine(); var props = new ExtendedProperties(); props.AddProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, _templatesPath); engine.Init(props); var template = engine.GetTemplate(name); template.Encoding = Encoding.UTF8.BodyName; var context = new VelocityContext(); var templateData = data ?? new Dictionary<string, object>(); foreach (var key in templateData.Keys) { context.Put(key, templateData[key]); } if (!string.IsNullOrEmpty(masterPage)) { context.Put("childContent", templateName); } using (var writer = new StringWriter()) { engine.MergeTemplate(name, context, writer); return writer.GetStringBuilder().ToString(); } }