Skip to content

Instantly share code, notes, and snippets.

@code-atom
Created August 30, 2017 05:24
Show Gist options
  • Save code-atom/5e301b1d44a15316f2347bb1fc0f7106 to your computer and use it in GitHub Desktop.
Save code-atom/5e301b1d44a15316f2347bb1fc0f7106 to your computer and use it in GitHub Desktop.
public class HandlebarHelper
{
public static void RegisterHelper()
{
RegisterArrayContainsHelper();
}
/// <summary>
/// {{#arrayContains List<> limit}}
/// ... if condition true
/// {{else}}
/// ... if condition false
/// {{/arrayContains}}
/// </summary>
private static void RegisterArrayContainsHelper()
{
Handlebars.RegisterHelper("arrayContains", (writer, helper, context, parameters) =>
{
if (parameters.Count() < 2)
{
writer.Write("arrayContains:Wrong number of arguments");
return;
}
var array = parameters[0] as IList;
var number = Convert.ToInt32(parameters[1]);
if (array.Count == number)
{
helper.Template(writer, context);
}
else
{
helper.Inverse(writer, context);
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment