Skip to content

BlockHelper inversion (^) throws #354

Closed
dotnet/arcade
#16884

Description

@oformaniuk

Scenario

Handlebars.js

Template

{{^test input}}empty{{else}}not empty{{/test}}

Helper

Handlebars.registerHelper("test", function(conditional, options) {
  if (conditional) {
    return options.fn(conditional);
  } else {
    return options.inverse(conditional);
  }
});

Input & result

{ "input": 1 } // Result: not empty
{ "input": 0 } // Result: empty

Handlebars.Net

[Fact]
public void BlockHelperWithInversion()
{
    string source = @"{{^test input}}empty{{else}}not empty{{/test}}";

    var handlebars = Handlebars.Create();
    handlebars.RegisterHelper("test", (output, options, context, arguments) =>
    {
        if (HandlebarsUtils.IsTruthy(arguments[0]))
        {
            options.Template(output, context);
        }
        else
        {
            options.Inverse(output, context);
        }
    });

    var template = handlebars.Compile(source);
    
    Assert.Equal("empty", template(null));
    Assert.Equal("empty", template(new { otherInput = 1 }));
    Assert.Equal("not empty", template(new { input = 1 }));
}

Actual result

  • ❌ ^test detected as HandlebarsHelper instead of HandlebarsBlockHelper
  • ❌ ^test tried to be late-bounded
  • ❌ Template references a helper that is not registered. Could not find helper '^test' exception thrown

Expected result

  • matches to Handlebars.js behaviour.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions