public static string GetAssemblyTemplate(string templateFullName, object data) { var assembly = Assembly.GetExecutingAssembly(); string resourceName = assembly.GetManifestResourceNames() .Single(str => str.EndsWith(templateFullName)); string result = ""; using (Stream stream = assembly.GetManifestResourceStream(resourceName)) using (StreamReader reader = new StreamReader(stream)) { result = reader.ReadToEnd(); } string template = ""; try { HtmlFormatCompiler compiler = new HtmlFormatCompiler(); string format = result; Generator generator = compiler.Compile(format); template = generator.Render(data); } catch (Exception) { template = "<h5 style='color:red'>Template rendering failed</h5>"; } return(template); }
public void ShouldIgnoreHTMLCharactersInsideTripleCurlyBraces() { HtmlFormatCompiler compiler = new HtmlFormatCompiler(); var generator = compiler.Compile("<html><body>Hello, {{{Name}}}!!!</body></html>"); string html = generator.Render(new { Name = "John \"The Man\" Standford" }); Assert.AreEqual("<html><body>Hello, John \"The Man\" Standford!!!</body></html>", html); }
public void ShouldEscapeValueContainingHTMLCharacters() { HtmlFormatCompiler compiler = new HtmlFormatCompiler(); var generator = compiler.Compile("<html><body>Hello, {{Name}}!!!</body></html>"); string html = generator.Render(new { Name = "John \"The Man\" Standford" }); Assert.AreEqual("<html><body>Hello, John "The Man" Standford!!!</body></html>", html); }
public void ShouldNotTouchValueContainingSingleCurlyBracesInsideTripleCurlyBraces() { HtmlFormatCompiler compiler = new HtmlFormatCompiler(); var generator = compiler.Compile("<html><head><style>{{{Style}}}</style></head><body><b>Bold</b> statement!</body></html>"); string html = generator.Render(new { Style = "b { color: red; }" }); Assert.AreEqual("<html><head><style>b { color: red; }</style></head><body><b>Bold</b> statement!</body></html>", html); }
protected override void CreateEmailBody() { ReadTemplate(); string logoImgContentId = AddImgToBodyBuilder(Path.GetFullPath(Path.Combine(OutputDirLocation, PathToLogo))); var compiler = new HtmlFormatCompiler(); var generator = compiler.Compile(BodyBuilder.HtmlBody); BodyBuilder.HtmlBody = generator.Render(new { link = BaseUrl, confirmationLink = ConfirmationLink, img1 = logoImgContentId }); }
protected string GeneratorXML(object data, string templatePath, string xsdNamespace, string xsdPath) { string template = File.ReadAllText(HttpContext.Current.Server.MapPath(templatePath)); HtmlFormatCompiler compiler = new HtmlFormatCompiler(); Generator generator = compiler.Compile(template); string resultXML = generator.Render(new { data }); //驗證產出的XML是否符合xsd的規範 XmlReaderSettings xmlReadersettings = new XmlReaderSettings(); xmlReadersettings.Schemas.Add(xsdNamespace, HttpContext.Current.Server.MapPath(xsdPath)); xmlReadersettings.ValidationType = ValidationType.Schema; XmlReader reader = XmlReader.Create(new StringReader(resultXML), xmlReadersettings); XmlDocument document = new XmlDocument(); document.Load(reader); return(resultXML); }
public static string GetFileTemplate(string templatePath, object data) { string template = ""; try { if (File.Exists(templatePath)) { template = File.ReadAllText(templatePath); } else { throw new Exception(); } HtmlFormatCompiler compiler = new HtmlFormatCompiler(); Generator generator = compiler.Compile(template); template = generator.Render(data); } catch (Exception ex) { template = $"<h5 style='color:red'>Template rendering failed {(JintAddons.debug?ex.Message:"")}</h5>"; } return(template); }
static void Main(string[] args) { string templatePath = @"template/templateA0401.txt"; String templateA0401 = File.ReadAllText(templatePath); HtmlFormatCompiler compiler = new HtmlFormatCompiler(); Generator generator = compiler.Compile(templateA0401); InvoiceA0401 InvoiceA0401 = new InvoiceA0401(); InvoiceA0401.InvoiceNumber = "QE00000000"; InvoiceA0401.InvoiceDate = "20171019"; InvoiceA0401.InvoiceTime = "16:20:17"; InvoiceA0401.SellerIdentifier = "1234567890"; InvoiceA0401.SellerName = "家裡蹲"; InvoiceA0401.BuyerIdentifier = "0123456789"; InvoiceA0401.BuyerName = "廁所蹲"; InvoiceA0401.GroupMark = "*"; InvoiceA0401.DonateMark = "0"; InvoiceA0401.InvoiceType = "07"; InvoiceA0401.SalesAmount = "0"; InvoiceA0401.TaxType = "1"; InvoiceA0401.TaxRate = "0.05"; InvoiceA0401.TaxAmount = "5"; InvoiceA0401.DiscountAmount = "0"; InvoiceA0401.TotalAmount = "105"; InvoiceA0401.DiscountAmount = "0"; InvoiceA0401.ProductItems = new List <InvoiceA0401ProductItem>(); InvoiceA0401ProductItem productItem = new InvoiceA0401ProductItem(); productItem.Description = "測試品項"; productItem.Quantity = "1"; productItem.Unit = "個"; productItem.UnitPrice = "100"; productItem.Amount = "100"; productItem.SequenceNumber = "1"; InvoiceA0401.ProductItems.Add(productItem); string resultXML = generator.Render(new { InvoiceA0401 = InvoiceA0401 }); //驗證產出的XML是否符合xsd的規範 XmlReaderSettings xmlReadersettings = new XmlReaderSettings(); xmlReadersettings.Schemas.Add("urn:GEINV:eInvoiceMessage:A0401:3.1", @"EInvoiceXSD/v31/A0401.xsd"); xmlReadersettings.ValidationType = ValidationType.Schema; XmlReader reader = XmlReader.Create(new StringReader(resultXML), xmlReadersettings); XmlDocument document = new XmlDocument(); try { document.Load(reader); Console.Out.WriteLine(String.Format("XML產出完成")); System.Diagnostics.Debug.WriteLine("XML產出完成"); System.Diagnostics.Debug.WriteLine(resultXML); } catch (XmlSchemaValidationException ex) { Console.Out.WriteLine(String.Format("<p>XML驗證錯誤: {0}</p>", ex.Message)); System.Diagnostics.Debug.WriteLine(String.Format("<p>XML驗證錯誤: {0}</p>", ex.Message)); } Console.Out.WriteLine(resultXML); Console.Out.WriteLine(); Console.Out.WriteLine("按任意健結束..."); Console.ReadKey(); //System.Threading.Thread.Sleep(2000); }