Handlebars-Net / Handlebars.Net

A real .NET Handlebars engine

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Using a block helper inside a sub expression of an if results in

mwpremiere opened this issue · comments

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

Hello @mwpremiere
Block helpers are not supported to be invoked as part of subexpression.