public void DynamicPartialWithParameters() { var source = "Hello, {{> (lookup name) first='Marc' last='Smith' }}!"; Handlebars.RegisterHelper("lookup", (output, context, arguments) => { output.WriteSafeString(arguments[0]); }); var template = Handlebars.Compile(source); using (var reader = new StringReader("{{first}} {{last}}")) { var partialTemplate = Handlebars.Compile(reader); Handlebars.RegisterTemplate("test", partialTemplate); } var data = new { name = "test" }; var result = template(data); Assert.Equal("Hello, Marc Smith!", result); }
public void BasicPartialWithIncompleteChildContextDoesNotFallback() { string source = "Hello, {{>person leadDev}}!"; var template = Handlebars.Compile(source); var data = new { firstName = "Pete", lastName = "Jones", leadDev = new { firstName = "Marc" } }; var partialSource = "{{firstName}} {{lastName}}"; using (var reader = new StringReader(partialSource)) { var partialTemplate = Handlebars.Compile(reader); Handlebars.RegisterTemplate("person", partialTemplate); } var result = template(data); Assert.Equal("Hello, Marc !", result); }
public void RegisterPartialsSample() { var partial = @"<a href=""/people/{{id}}"">{{name}}</a>"; var source = @" <ul> {{#people}} <li>{{> link}}</li> {{/people}} </ul>"; var context = new { people = new[] { new { name = "Alan", id = 1 }, new { name = "John", id = 2 } } }; using (var handleBars = new Handlebars()) { handleBars.RegisterPartial("link", partial); handleBars.RegisterTemplate("myTemplate", source); Approvals.Verify(handleBars.Transform("myTemplate", context)); } }
/** * Performs a test by generating an invoice PDF using IronPDf. **/ private static async Task TestInvoiceAsync() { // generate invoice data var invoice = InvoiceHelper.GenerateRandomInvoice(10); // load templates var pageTemplate = await File.ReadAllTextAsync("Templates/Invoice.html"); var rowTemplate = await File.ReadAllTextAsync("Templates/InvoiceRow.html"); Handlebars.RegisterTemplate("positionRow", rowTemplate); // compile templates var template = Handlebars.Compile(pageTemplate); var result = template(invoice); // render to PDF using (var renderer = new HtmlToPdf { PrintOptions = { FirstPageNumber = 1, Footer = new SimpleHeaderFooter { DrawDividerLine = true, RightText = "Page {page} of {total-pages}" } } }) { var baseUri = new Uri(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Templates")); var pdf = await renderer.RenderHtmlAsPdfAsync(result, baseUri); pdf.SaveAs(_targetFilePath); } }
public void RegisterHelperSample() { var helperjs = @"function() { return new Handlebars.SafeString(""<a href='"" + this.url + ""'>"" + this.body + ""</a>""); }"; var source = @" <ul> {{#posts}} <li>{{link_to}}</li> {{/posts}} </ul>"; var context = new { posts = new[] { new { url = "/hello-world", body = "Hello World!" } } }; using (var handleBars = new Handlebars()) { handleBars.RegisterHelper("link_to", helperjs); handleBars.RegisterTemplate("myTemplate", source); Approvals.Verify(handleBars.Transform("myTemplate", context)); } }
public void Sample() { var source = @" <p>Hello, my name is {{name}}. I am from {{hometown}}. I have {{kids.length}} kids:</p> <ul> {{#kids}} <li>{{name}} is {{age}}</li> {{/kids}} </ul>"; var context = new { name = "Alan", hometown = "Somewhere, TX", kids = new[] { new { name = "Sally", age = "4" } } }; using (var handleBars = new Handlebars()) { handleBars.RegisterTemplate("Index", source); Approvals.Verify(handleBars.Transform("Index", context)); } }
public void BasicBlockPartial() { string source = "Hello, {{#>person1}}friend{{/person1}}!"; var template = Handlebars.Compile(source); var data = new { firstName = "Pete", lastName = "Jones" }; var result1 = template(data); Assert.AreEqual("Hello, friend!", result1); var partialSource = "{{firstName}} {{lastName}}"; using (var reader = new StringReader(partialSource)) { var partialTemplate = Handlebars.Compile(reader); Handlebars.RegisterTemplate("person1", partialTemplate); } var result2 = template(data); Assert.AreEqual("Hello, Pete Jones!", result2); }
public void BlockPartialWithNestedSpecialNamedPartial2() { string source = "A {{#>partial1}} B {{#>partial2}} {{VarC}} {{/partial2}} D {{/partial1}} E"; var template = Handlebars.Compile(source); var partialSource1 = "1 {{> @partial-block }} 2"; using (var reader = new StringReader(partialSource1)) { var partialTemplate = Handlebars.Compile(reader); Handlebars.RegisterTemplate("partial1", partialTemplate); } var partialSource2 = "3 {{> @partial-block }} 4"; using (var reader = new StringReader(partialSource2)) { var partialTemplate = Handlebars.Compile(reader); Handlebars.RegisterTemplate("partial2", partialTemplate); } var data = new { VarC = "C" }; var result = template(data); Assert.Equal("A 1 B 3 C 4 D 2 E", result); }
public void BlockPartialWithNestedSpecialNamedPartial() { string source = "Well, {{#>partial1}}some test{{/partial1}} !"; var template = Handlebars.Compile(source); var partialSource1 = "this is {{> @partial-block }} content {{#>partial2}}works{{/partial2}} {{lastName}}"; using (var reader = new StringReader(partialSource1)) { var partialTemplate = Handlebars.Compile(reader); Handlebars.RegisterTemplate("partial1", partialTemplate); } var partialSource2 = "that {{> @partial-block}} great {{firstName}}"; using (var reader = new StringReader(partialSource2)) { var partialTemplate = Handlebars.Compile(reader); Handlebars.RegisterTemplate("partial2", partialTemplate); } var data = new { firstName = "Pete", lastName = "Jones" }; var result = template(data); Assert.Equal("Well, this is some test content that works great Pete Jones !", result); }
public void BasicBlockPartialWithArgument() { string source = "Hello, {{#>person2 arg='Todd'}}friend{{/person2}}!"; var template = Handlebars.Compile(source); var data = new { firstName = "Pete", lastName = "Jones" }; var result1 = template(data); Assert.Equal("Hello, friend!", result1); var partialSource = "{{arg}}"; using (var reader = new StringReader(partialSource)) { var partialTemplate = Handlebars.Compile(reader); Handlebars.RegisterTemplate("person2", partialTemplate); } var result2 = template(data); Assert.Equal("Hello, Todd!", result2); }
public void BasicPartialWithContextAndStringParameters() { string source = "Hello, {{>person first=leadDev.marc last='Smith'}}!"; var template = Handlebars.Compile(source); var data = new { leadDev = new { marc = new { name = "Marc" } } }; var partialSource = "{{first.name}} {{last}}"; using (var reader = new StringReader(partialSource)) { var partialTemplate = Handlebars.Compile(reader); Handlebars.RegisterTemplate("person", partialTemplate); } var result = template(data); Assert.Equal("Hello, Marc Smith!", result); }
static HandlebarsCache() { // Register all partials List <string> partials = new List <string>(); // Match all directories under "Views" with "Partials" in the name foreach (var directory in Directory.EnumerateDirectories("Views", "Partials", SearchOption.AllDirectories)) { // Register all hbs files within the directory foreach (var path in Directory.EnumerateFiles(directory, "*.hbs", SearchOption.TopDirectoryOnly)) { string name = Path.GetFileNameWithoutExtension(path); if (partials.Exists(p => p.Equals(name, StringComparison.OrdinalIgnoreCase))) { throw new Exception("Duplicate partial view: '" + name + "' (" + path + "). All partial views must be uniquely named"); } else { partials.Add(path); } string partialTemplate = File.ReadAllText(path); Handlebars.RegisterTemplate(name, partialTemplate); } } }
public void BasicPartialWithSubExpressionParameters() { string source = "Hello, {{>person first=(_ first arg1=(_ \"value\")) last=(_ last)}}!"; Handlebars.RegisterHelper("_", (output, context, arguments) => { output.Write(arguments[0].ToString()); if (arguments.Length > 1) { var hash = arguments[1] as Dictionary <string, object>; output.Write(hash["arg1"]); } }); var template = Handlebars.Compile(source); var partialSource = "{{first}} {{last}}"; using (var reader = new StringReader(partialSource)) { var partialTemplate = Handlebars.Compile(reader); Handlebars.RegisterTemplate("person", partialTemplate); } var result = template(new { first = 1, last = true }); Assert.Equal("Hello, 1value True!", result); }
public void BasicPartialWithCustomBlockHelper() { string source = "Hello, {{>person title='Mr.'}}!"; var template = Handlebars.Compile(source); var data = new { firstName = "Pete", lastName = "Jones", }; Handlebars.RegisterHelper("block", (writer, root, options, context, parameters) => options.Template(writer, context)); var partialSource = "{{#block}}{{title}} {{firstName}} {{lastName}}{{/block}}"; using (var reader = new StringReader(partialSource)) { var partialTemplate = Handlebars.Compile(reader); Handlebars.RegisterTemplate("person", partialTemplate); } var result = template(data); Assert.Equal("Hello, Mr. Pete Jones!", result); }
private string ComposeBodyAndLayout(T context, BodyBuilder builder) { foreach (var link in context.LinkedResources) { var res = builder.LinkedResources.Add(link.Path); res.ContentId = link.Cid; Handlebars.RegisterTemplate(link.Name, link.Cid); } // Read partial template for the body and register it var templatePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Views\" + this.MailerName + @"\" + _message.ViewName.ToLower() + ".html"); var source = File.ReadAllText(templatePath); Handlebars.RegisterTemplate("body", source); // Read layout template and compose partial template string layoutName = this.LayoutName; var hasExtension = this.LayoutName.IndexOf(".html") > -1; if (!hasExtension) { layoutName = this.LayoutName + ".html"; } var layoutPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Views\" + this.MailerName + @"\" + layoutName); var layoutSource = File.ReadAllText(layoutPath); var layoutTemplate = Handlebars.Compile(layoutSource); return(layoutTemplate(context)); }
public void Execute() { var root = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location.Substring(0, Assembly.GetEntryAssembly().Location.IndexOf("bin\\"))); var basePropes = new[] { "IsDeleted" }; string source = File.ReadAllText(root + TemplateDirectory + "CSharpEntityType/Class.hbs"); string importsSource = File.ReadAllText(root + TemplateDirectory + "CSharpEntityType/Partials/Imports.hbs"); string constructorSource = File.ReadAllText(root + TemplateDirectory + "CSharpEntityType/Partials/Constructor.hbs"); string propertiesSource = File.ReadAllText(root + TemplateDirectory + "CSharpEntityType/Partials/Properties1.hbs"); Handlebars.RegisterTemplate("constructor", constructorSource); Handlebars.RegisterTemplate("properties", propertiesSource); Handlebars.RegisterTemplate("imports", importsSource); Handlebars.RegisterHelper("spaces", (w, c, p) => { string s = ""; for (int i = 0; i < Convert.ToInt32(p[0]); i++) { s += " "; } w.Write(s); }); var template = Handlebars.Compile(source); SqlConnectionHelper helper = new SqlConnectionHelper(); var tables = helper.GetTableDefinitions(); var datas = new List <dynamic>(); tables.ForEach(x => { datas.Add(new { base_class = "BaseEntity", @namespace = "Models", @class = x.Name, properties = x.ColumnDefinitions.Where(x => !basePropes.Contains(x.Name) && !x.IsForeign).Select(x => new { propertyType = ResolveTypeName(x.Type), propertyName = x.Name }), foreignProperties = x.ColumnDefinitions.Where(x => !basePropes.Contains(x.Name) && x.IsForeign).Select(x => new { propertyType = ResolveTypeName(x.Type), propertyName = x.Name, foreign = x.ForeignEntity }) }); }); foreach (var data in datas) { var result = template(data); var folder = Path.Combine(root, "Models"); var path = Path.Combine(folder, data.@class + ".cs"); if (!Directory.Exists(folder)) { Directory.CreateDirectory(folder); } using (StreamWriter writer = new StreamWriter(path, false, Encoding.UTF8)) { writer.WriteLine(result); } //File.AppendAllText(path, result); } }
public void NumberAtStartOfTemplateName() { using (var handleBars = new Handlebars()) { var argumentException = Assert.Throws <ArgumentException>(() => { handleBars.RegisterTemplate("1Index", "AA"); }); Assert.AreEqual("'templateName' cannot start with a number.\r\nParameter name: templateName", argumentException.Message); } }
protected void AddPartial(string partialName, string source) { using (var reader = new StringReader(source)) { var template = Handlebars.Compile(reader); Handlebars.RegisterTemplate(partialName, template); } }
public void CaseInsensitive() { var source = "Foo"; using (var handleBars = new Handlebars()) { handleBars.RegisterTemplate("mytemplate", source); Approvals.Verify(handleBars.Transform("myTemplate", null)); } }
public void NewLineInTemplate() { var source = "AA\r\nBB"; using (var handleBars = new Handlebars()) { handleBars.RegisterTemplate("Index", source); Approvals.Verify(handleBars.Transform("Index", null)); } }
/// <summary> /// Compile template file and include it in result /// /// Template file should be in "./templates" directory and be named same as template variable /// starting with underscore /// </summary> /// <param name="partialName"></param> private static void CompilePartial(string partialName) { var templateRaw = File.ReadAllText($"./templates/_{partialName}.hbs"); using (var reader = new StringReader(templateRaw)) { var template = Handlebars.Compile(reader); Handlebars.RegisterTemplate(partialName, template); } }
public void NumberInTemplateName() { var source = "AA\r\nBB"; using (var handleBars = new Handlebars()) { handleBars.RegisterTemplate("Index1", source); Approvals.Verify(handleBars.Transform("Index1", null)); } }
private HandlebarsTemplate <object, object> CompileMustache() { foreach (var partial in Partials) { using (var reader = new StringReader(partial.Value)) { var template = Handlebars.Compile(reader); Handlebars.RegisterTemplate(partial.Key, template); } } return(Handlebars.Compile(Mustache)); }
public void MissingPartial() { var partial = @"<a href=""/people/{{id}}"">{{name}}</a>"; var source = "{{> partial}}"; using (var handleBars = new Handlebars()) { // handleBars.RegisterPartial("link", partial); handleBars.RegisterTemplate("myTemplate", source); Approvals.Verify(handleBars.Transform("myTemplate", null)); } }
public void PartialWithNewline() { var partial = "thepartial\r\n"; var source = @"<li>{{> partial}}</li>"; using (var handleBars = new Handlebars()) { handleBars.RegisterPartial("partial", partial); handleBars.RegisterTemplate("myTemplate", source); Approvals.Verify(handleBars.Transform("myTemplate", new object())); } }
private static void RegisterAllTemplates() { foreach (var file in Directory.GetFiles(ViewsDirectory, "*" + Strings.TemplateExt)) { var viewName = Path.GetFileNameWithoutExtension(file); var viewData = File.ReadAllText(file); Handlebars.RegisterTemplate(viewName, viewData); if (!_templates.ContainsKey(viewName)) { _templates.Add(viewName, viewData); } } }
public Task <string> BuildAsync(string template, object data, IDictionary <string, string> partials = null) { if (partials != null) { foreach (var partial in partials) { Handlebars.RegisterTemplate(partial.Key, partial.Value); } } var builder = Handlebars.Compile(template); var result = builder(data); return(Task.FromResult(result)); }
public CommandBase() { this.CurrentPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); this.AssemblyVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString(); var data = new { toolName = Constants.CLIName, version = this.AssemblyVersion }; var templateSource = File.ReadAllText(Path.Combine(this.CurrentPath, Constants.TemplatesFolderName, "Sign.Template")); var template = Handlebars.Compile(templateSource); this.Sign = template(data); Handlebars.RegisterTemplate("sign", templateSource); }
private string NoLayout(T context, BodyBuilder builder) { foreach (var link in context.LinkedResources) { var res = builder.LinkedResources.Add(link.Path); var cid = res.ContentId; link.Cid = cid; Handlebars.RegisterTemplate(link.Name, link.Cid); } var templatePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Views\" + this.MailerName + @"\" + _message.ViewName.ToLower() + ".html"); var source = File.ReadAllText(templatePath); var template = Handlebars.Compile(source); var result = template(context); return(result); }
public void BasicPartialWithStringParameter() { string source = "Hello, {{>person first='Pete'}}!"; var template = Handlebars.Compile(source); var partialSource = "{{first}}"; using (var reader = new StringReader(partialSource)) { var partialTemplate = Handlebars.Compile(reader); Handlebars.RegisterTemplate("person", partialTemplate); } var result = template(null); Assert.Equal("Hello, Pete!", result); }
public void BlockPartialWithSpecialNamedPartial() { string source = "Well, {{#>myPartial}}some test{{/myPartial}} !"; var template = Handlebars.Compile(source); var partialSource = "this is {{> @partial-block }} content"; using (var reader = new StringReader(partialSource)) { var partialTemplate = Handlebars.Compile(reader); Handlebars.RegisterTemplate("myPartial", partialTemplate); } var data = new { }; var result = template(data); Assert.Equal("Well, this is some test content !", result); }
public void RegisterPartialsSample() { var partial = @"<a href=""/people/{{id}}"">{{name}}</a>"; var source = @" <ul> {{#people}} <li>{{> link}}</li> {{/people}} </ul>"; var context = new { people = new[] { new { name = "Alan", id = 1 }, new { name = "Yehuda", id = 2 } } }; using (var handleBars = new Handlebars()) { handleBars.RegisterPartial("link", partial); handleBars.RegisterTemplate("myTemplate", source); Approvals.Verify(handleBars.Transform("myTemplate", context)); } }