Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions README.md
Original file line numberDiff line numberDiff line change
Expand Up@@ -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<string>(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<string, string>()
Expand All@@ -141,8 +144,8 @@ var animals = new Dictionary<string, string>()
{"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<object, string> 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
Expand Down
44 changes: 44 additions & 0 deletions source/Handlebars.Test/ReadmeTests.cs
Original file line numberDiff line numberDiff line change
@@ -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<string>(0);
var right = arguments[1] as string;
if (left == right) options.Template(output, context);
else options.Inverse(output, context);
});

var animals = new Dictionary<string, string>
{
{"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
);
}
}
}