diff --git a/README.md b/README.md index 8894f5e5..a0402100 100644 --- a/README.md +++ b/README.md @@ -124,14 +124,17 @@ Views\{Controller}\{Action}\partials\somepartial.hbs ### Registering Block Helpers ```c# -Handlebars.RegisterHelper("StringEqualityBlockHelper", (TextWriter output, HelperOptions options, dynamic context, object[] arguments) => +Handlebars.RegisterHelper("StringEqualityBlockHelper", (output, options, context, arguments) => { - if (arguments.Length != 2) throw new HandlebarsException("{{StringEqualityBlockHelper}} helper must have exactly two argument"); - - var left = arguments[0] as string; + if (arguments.Length != 2) + { + throw new HandlebarsException("{{#StringEqualityBlockHelper}} helper must have exactly two arguments"); + } + + var left = arguments.At(0); var right = arguments[1] as string; - if (left == right) options.Template(output, null); - else options.Inverse(output, null); + if (left == right) options.Template(output, context); + else options.Inverse(output, context); }); var animals = new Dictionary() @@ -141,8 +144,8 @@ var animals = new Dictionary() {"Chewy", "hamster" } }; -string template = "{{#each this}}The animal, {{@key}}, {{#StringEqualityBlockHelper @value 'dog'}}is a dog{{else}}is not a dog{{/StringEqualityBlockHelper}}.\r\n{{/each}}"; -Func compiledTemplate = Handlebars.Compile(template); +var template = "{{#each this}}The animal, {{@key}}, {{#StringEqualityBlockHelper @value 'dog'}}is a dog{{else}}is not a dog{{/StringEqualityBlockHelper}}.\r\n{{/each}}"; +var compiledTemplate = Handlebars.Compile(template); string templateOutput = compiledTemplate(animals); /* Would render diff --git a/source/Handlebars.Test/ReadmeTests.cs b/source/Handlebars.Test/ReadmeTests.cs new file mode 100644 index 00000000..07462818 --- /dev/null +++ b/source/Handlebars.Test/ReadmeTests.cs @@ -0,0 +1,44 @@ +using System.Collections.Generic; +using Xunit; + +namespace HandlebarsDotNet.Test +{ + public class ReadmeTests + { + [Fact] + public void RegisterBlockHelper() + { + var handlebars = Handlebars.Create(); + handlebars.RegisterHelper("StringEqualityBlockHelper", (output, options, context, arguments) => + { + if (arguments.Length != 2) + { + throw new HandlebarsException("{{#StringEqualityBlockHelper}} helper must have exactly two arguments"); + } + + var left = arguments.At(0); + var right = arguments[1] as string; + if (left == right) options.Template(output, context); + else options.Inverse(output, context); + }); + + var animals = new Dictionary + { + {"Fluffy", "cat" }, + {"Fido", "dog" }, + {"Chewy", "hamster" } + }; + + var template = "{{#each this}}The animal, {{@key}}, {{#StringEqualityBlockHelper @value 'dog'}}is a dog{{else}}is not a dog{{/StringEqualityBlockHelper}}.\r\n{{/each}}"; + var compiledTemplate = handlebars.Compile(template); + string templateOutput = compiledTemplate(animals); + + Assert.Equal( + "The animal, Fluffy, is not a dog.\r\n" + + "The animal, Fido, is a dog.\r\n" + + "The animal, Chewy, is not a dog.\r\n", + templateOutput + ); + } + } +} \ No newline at end of file