public void CanCompileAndRunLater() { var service = new HandlebarsService(); service.Compile("template-name", "Hello {{name}}"); Assert.Equal("Hello Matt", service.Run("template-name", new { name = "Matt" })); }
public void CanCreateReusableTemplateDelegate() { var service = new HandlebarsService(); var func = service.Compile("Hello {{name}}"); Assert.Equal("Hello Matt", func(new { name = "Matt" })); Assert.Equal("Hello Stuart", func(new { name = "Stuart" })); }
public void CanRenderPartialWithChildModel() { HandlebarsService.RegisterPartial("body", "{{forename}} {{surname}}"); string template = "{{>body person}}"; string expected = "Matthew Abbott"; RenderTest(template, expected, new { person = new { forename = "Matthew", surname = "Abbott" } }); }
public void CanRenderPartial() { HandlebarsService.RegisterPartial("body", "Hello World"); string template = "{{>body}}"; string expected = "Hello World"; RenderTest(template, expected); }
public void SupportsDynamicEnumerableModel() { string template = "<ul>{{#each this}}<li>{{value}}</li>{{/each}}</ul>"; dynamic model = new dynamic[] { new { value = 1 }, new { value = 2 } }; string expected = "<ul><li>1</li><li>2</li></ul>"; var service = new HandlebarsService(); string result = service.CompileAndRun("my-template", template, model); Assert.Equal(expected, result); }
public void SupportsPocoModel() { var person = new Person() { Forename = "Matt", Surname = "Abbott", Age = 30 }; string template = "{{Forename}} {{Surname}} is {{Age}} years old"; string expected = "Matt Abbott is 30 years old"; var service = new HandlebarsService(); string result = service.CompileAndRun("my-template", template, person); Assert.Equal(expected, result); }
public void SupportsDynamicModel() { dynamic person = new { Forename = "Matt", Surname = "Abbott", Age = 30 }; string template = "{{Forename}} {{Surname}} is {{Age}} years old"; string expected = "Matt Abbott is 30 years old"; var service = new HandlebarsService(); string result = service.CompileAndRun("my-template", template, person); Assert.Equal(expected, result); }
public void SupportsDynamicEnumerableModel() { string template = "<ul>{{#each this}}<li>{{value}}</li>{{/each}}</ul>"; dynamic model = new dynamic[] { new { value = 1 }, new { value = 2} }; string expected = "<ul><li>1</li><li>2</li></ul>"; var service = new HandlebarsService(); string result = service.CompileAndRun("my-template", template, model); Assert.Equal(expected, result); }
public void SupportsRootLookupInParameter() { var service = new HandlebarsService(); var model = new { property = "Hello" }; string template = "{{#with @root.property}}{{this}}{{/with}}"; string expected = "Hello"; Assert.Equal(expected, service.CompileAndRun("test", template, model)); }
public void SupportsDynamicExpandoObjectModel() { string template = "{{Forename}} {{Surname}}"; dynamic model = new ExpandoObject(); model.Forename = "Matthew"; model.Surname = "Abbott"; string expected = "Matthew Abbott"; var service = new HandlebarsService(); string result = service.CompileAndRun("my-template", template, model); Assert.Equal(expected, result); }
public void CanRunExpressionHelper() { var service = new HandlebarsService(); service.RegisterHelper("fullname", options => string.Format("{0} {1}", options.Data.forename, options.Data.surname)); string template = "Your full name is: {{fullname person}}"; string expected = "Your full name is: Matthew Abbott"; var model = new { person = new { forename = "Matthew", surname = "Abbott" } }; Assert.Equal(expected, service.CompileAndRun("test", template, model)); }
public void CanRunUnknownValueResolver() { var service = new HandlebarsService(); UnknownValueResolver resolver = (e) => (e == "name" ? "Matthew Abbott" : null); string template = "Your full name is: {{name}}"; string expected = "Your full name is: Matthew Abbott"; var model = new { }; Assert.Equal(expected, service.CompileAndRun("test", template, model, resolver)); }
public void CanAccessHashParametersInHelper() { var service = new HandlebarsService(); service.RegisterHelper("formatted_name", options => { return "<h1 class=\"" + options.Hash["class"] + "\">" + options.Fn(options.Data) + "</h1>"; }); string template = "{{#formatted_name class=\"test\"}}<strong>{{name}}</strong>{{/formatted_name}}"; var model = new { name = "Matt" }; string result = service.CompileAndRun("test", template, model); Assert.Equal("<h1 class=\"test\"><strong>Matt</strong></h1>", result); }
public void CanAccessHashParametersInHelper() { var service = new HandlebarsService(); service.RegisterHelper("formatted_name", options => { return("<h1 class=\"" + options.Hash["class"] + "\">" + options.Fn(options.Data) + "</h1>"); }); string template = "{{#formatted_name class=\"test\"}}<strong>{{name}}</strong>{{/formatted_name}}"; var model = new { name = "Matt" }; string result = service.CompileAndRun("test", template, model); Assert.Equal("<h1 class=\"test\"><strong>Matt</strong></h1>", result); }
public void SupportsRootLookupThroughPartial() { var service = new HandlebarsService(); string template = "Your name is {{>person_name}}"; var model = new { world = "World", person = new { forename = "Matthew", surname = "Abbott" } }; string partial = "{{@root.person.forename}} {{@root.person.surname}}"; service.RegisterPartial("person_name", partial); service.Compile("hello-world", template); string result = service.Run("hello-world", model); string expected = "Your name is Matthew Abbott"; Assert.Equal(expected, result); }
public void CanRunBlockHelper() { var service = new HandlebarsService(); service.RegisterHelper("list", options => { var enumerable = options.Data as IEnumerable ?? new [] {options.Data}; return "<ul>" + string.Join("", enumerable.OfType<object>().Select(options.Fn)) + "</ul>"; }); string template = "{{#list this}}<li>{{name}}</li>{{/list}}"; var model = new[] { new { name = "Matt" }, new { name = "Stuart" } }; string result = service.CompileAndRun("test", template, model); Assert.Equal("<ul><li>Matt</li><li>Stuart</li></ul>", result); }
public void CanRunBlockHelper() { var service = new HandlebarsService(); service.RegisterHelper("list", options => { var enumerable = options.Data as IEnumerable ?? new [] { options.Data }; return("<ul>" + string.Join("", enumerable.OfType <object>().Select(options.Fn)) + "</ul>"); }); string template = "{{#list this}}<li>{{name}}</li>{{/list}}"; var model = new[] { new { name = "Matt" }, new { name = "Stuart" } }; string result = service.CompileAndRun("test", template, model); Assert.Equal("<ul><li>Matt</li><li>Stuart</li></ul>", result); }
public void SupportsRootLookup() { var service = new HandlebarsService(); var model = new { property = "Hello", other = new { forename = "Matt", surname = "Abbott", job = new { title = "Developer" } } }; string template = "{{#with other}}{{@root.property}} {{forename}} {{surname}}, {{job.title}}{{/with}}"; string expected = "Hello Matt Abbott, Developer"; Assert.Equal(expected, service.CompileAndRun("test", template, model)); }
public void CanCompileAndRunInOneCall() { var service = new HandlebarsService(); Assert.Equal("Hello Matt", service.CompileAndRun("my-template", "Hello {{name}}", new { name = "Matt" })); }