using System; using System.IO; using webapi.Infrastructure.Model; using NVelocity.App; using Commons.Collections; using NVelocity.Runtime.Resource; using NVelocity.Runtime.Resource.Loader; using NVelocity.Runtime.Directive; using NVelocity; namespace webapi.Infractructure.Helpers { public static class TemplateHelper { public static string Generate(Letter letter, string template) { //try { return generateImpl(letter, template); } //catch (Exception) //{ // // todo: log // return string.Empty; //} } private static string generateImpl(Letter letter, string templateName) { var velocity = new VelocityEngine(); var props = new ExtendedProperties(); string templateFolderAbsolutePath = System.Web.Hosting.HostingEnvironment.MapPath("~\\bin\\Templates"); // workaround for using NVelocity from GAC, not from bin folder props.AddProperty("file.resource.loader.path", templateFolderAbsolutePath); props.AddProperty("resource.manager.class", typeof(ResourceManagerImpl).AssemblyQualifiedName); props.AddProperty("file.resource.loader.class", typeof(FileResourceLoader).AssemblyQualifiedName); props.AddProperty("directive.manager", typeof(DirectiveManager).AssemblyQualifiedName); velocity.Init(props); var template = velocity.GetTemplate(templateName); var context = new VelocityContext(); context.Put("letter", letter); using (var writer = new StringWriter()) { template.Merge(context, writer); var result = writer.GetStringBuilder().ToString(); return result; } } } }