From e3bd448bb8927a57caa0b38b03dc943c26d05d89 Mon Sep 17 00:00:00 2001 From: Sam Ferencik Date: Tue, 23 Jun 2020 12:34:43 +0200 Subject: [PATCH 1/3] Fix syntax for block helpers --- README.md | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 8894f5e5..a4e91515 100644 --- a/README.md +++ b/README.md @@ -124,24 +124,29 @@ Views\{Controller}\{Action}\partials\somepartial.hbs ### Registering Block Helpers ```c# -Handlebars.RegisterHelper("StringEqualityBlockHelper", (TextWriter output, HelperOptions options, dynamic context, object[] arguments) => -{ - if (arguments.Length != 2) throw new HandlebarsException("{{StringEqualityBlockHelper}} helper must have exactly two argument"); - - var left = arguments[0] as string; - var right = arguments[1] as string; - if (left == right) options.Template(output, null); - else options.Inverse(output, null); -}); - -var animals = new Dictionary() -{ +HandlebarsBlockHelper _stringEqualityBlockHelper = (TextWriter output, HelperOptions options, dynamic context, object[] arguments) => { + if (arguments.Length != 2) + { + throw new HandlebarsException("{{#StringEqualityBlockHelper}} helper must have exactly two arguments"); + } + string left = arguments[0] as string; + string right = arguments[1] as string; + if (left == right) + { + options.Template(output, null); + } + else + { + options.Inverse(output, null); + } +}; +Handlebars.RegisterHelper("StringEqualityBlockHelper", _stringEqualityBlockHelper); +Dictionary animals = new Dictionary() { {"Fluffy", "cat" }, {"Fido", "dog" }, {"Chewy", "hamster" } }; - -string template = "{{#each this}}The animal, {{@key}}, {{#StringEqualityBlockHelper @value 'dog'}}is a dog{{else}}is not a dog{{/StringEqualityBlockHelper}}.\r\n{{/each}}"; +string template = "{{#each @value}}The animal, {{@key}}, {{#StringEqualityBlockHelper @value 'dog'}}is a dog{{else}}is not a dog{{/StringEqualityBlockHelper}}.\r\n{{/each}}"; Func compiledTemplate = Handlebars.Compile(template); string templateOutput = compiledTemplate(animals); From 12ce419c7dbb325cea35f8e4311c64828001f607 Mon Sep 17 00:00:00 2001 From: Oleh Formaniuk Date: Sat, 7 Nov 2020 17:15:50 +0200 Subject: [PATCH 2/3] Update readme according to latest changes --- README.md | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index a4e91515..a0402100 100644 --- a/README.md +++ b/README.md @@ -124,30 +124,28 @@ Views\{Controller}\{Action}\partials\somepartial.hbs ### Registering Block Helpers ```c# -HandlebarsBlockHelper _stringEqualityBlockHelper = (TextWriter output, HelperOptions options, dynamic context, object[] arguments) => { - if (arguments.Length != 2) - { - throw new HandlebarsException("{{#StringEqualityBlockHelper}} helper must have exactly two arguments"); - } - string left = arguments[0] as string; - string right = arguments[1] as string; - if (left == right) - { - options.Template(output, null); - } - else - { - options.Inverse(output, null); - } -}; -Handlebars.RegisterHelper("StringEqualityBlockHelper", _stringEqualityBlockHelper); -Dictionary animals = new Dictionary() { +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" } }; -string template = "{{#each @value}}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 From 26f44111b1014725ff0cec24c49f8bd89c09eb0d Mon Sep 17 00:00:00 2001 From: Oleh Formaniuk Date: Sat, 7 Nov 2020 17:16:30 +0200 Subject: [PATCH 3/3] Add test to cover `RegisterBlockHelper` readme example --- source/Handlebars.Test/ReadmeTests.cs | 44 +++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 source/Handlebars.Test/ReadmeTests.cs 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