Handlebars-Net / Handlebars.Net.Helpers

Handlebars.Net helpers in the categories: 'Boolean', 'Constants', 'Enumerable', 'Environment', 'Math', 'Regex', 'String', 'DateTime' and 'Url'.
MIT License
40 stars 15 forks source link

I can't seem to use TitleCase against a dynamic expression #43

Closed foconnor-DS closed 1 year ago

foconnor-DS commented 2 years ago

Am I missing something?

 class Program
    {
        static void Main(string[] args)
        {
            var handlebars = Handlebars.Create();
            HandlebarsHelpers.Register(handlebars, options => { options.UseCategoryPrefix = false; });
            var data = new Dictionary<string, dynamic>
            {
                { "FirstName", "jill" }
            };
            var test = "{{Titlecase [FirstName]}}";
            var template = handlebars.Compile(test);
            var result = template.Invoke(template, data);
            Console.WriteLine($"{result}");
        }
    }

This throws:

Object of type 'HandlebarsDotNet.UndefinedBindingResult' cannot be converted to type 'System.String'

I notice all the unit tests and examples are using string literals. Are these helpers supposed to work with dynamic varibles?

StefH commented 2 years ago

@foconnor-DS I'll investigate.

(Do you know if the default Handlebars.Net does support dynamic)?

StefH commented 1 year ago

@foconnor-DS This works fine:

[Fact]
    public void TitleCase_Dynamic()
    {
        // Arrange
        var data = new Dictionary<string, dynamic>
        {
            { "FirstName", "jill" }
        };
        var test = "{{[String.Titlecase] [FirstName]}}";
        var action = _handlebarsContext.Compile(test);

        // Act
        var result = action(data);

        // Assert
        result.Should().Be("Jill");
    }
StefH commented 1 year ago

https://github.com/Handlebars-Net/Handlebars.Net.Helpers/pull/75