Using a block helper inside a sub expression of an if results in
mwpremiere opened this issue · comments
mwpremiere commented
Describe the bug
Using a block helper inside a sub expression of an if results in the error " Template references a helper that cannot be resolved. Helper <Helper Name Here>"
This works: {{#StringEquals 'test' 'test'}}{{/StringEquals}}
This Fails: {{#if (StringEquals 'test' 'test')}}{{/if}}
Expected behavior:
Sub expression should resolve as expected
Test to reproduce
[Test]
public void TestFails()
{
Handlebars.RegisterHelper("StringEquals", StringEqualsHelper);
var template = Handlebars.Compile("{{#if (StringEquals 'test' 'test')}}Test{{/if}}");
var data = new
{
Test = 123,
};
template.Invoke(data);
}
[Test]
public void TestWorks()
{
Handlebars.RegisterHelper("StringEquals", StringEqualsHelper);
var template = Handlebars.Compile("{{#StringEquals 'test' 'test'}}Test{{/StringEquals}}");
var data = new
{
Test = 123,
};
template.Invoke(data);
}
private void StringEqualsHelper(EncodedTextWriter output, BlockHelperOptions options, HandlebarsDotNet.Context context, Arguments arguments)
{
if (arguments.Length != 2)
{
throw new HandlebarsException("{{#" + "StringEquals" + "}} helper must have exactly two arguments");
}
var left = arguments.At<string>(0);
var right = arguments.At<string>(1);
if (left == right)
options.Template(output, context);
else
options.Inverse(output, context);
}
Other related info
Using .NET core 3.1
Oleh Formaniuk commented
Hello @mwpremiere
Block helpers are not supported to be invoked as part of subexpression.