Handlebars-Net / Handlebars.Net.Helpers

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

Add examples of using the String.Split helper in a #each expression #53

Closed SeanFarrow closed 1 year ago

SeanFarrow commented 2 years ago

I am trying to use the String.Split helper in an #each expression, but can't seem to find the correct syntax.

Does anyone have any examples?

Thanks, Sean.

kspearrin commented 1 year ago

I can't seem to get this to work either. Example:

Template

{{String.Split "a;b;c" ";"}}

{{#each (String.Split "a;b;c" ";")}}
{{@Key}}:{{@Index}}:{{this}}
{{/each}}

Result

["a","b","c"]

Length:0:13
StefH commented 1 year ago

@SeanFarrow and @kspearrin

Did you check the unit test?

https://github.com/Handlebars-Net/Handlebars.Net.Helpers/blob/master/test/Handlebars.Net.Helpers.Tests/Templates/StringHelpersTemplateTests.cs#L95

kspearrin commented 1 year ago

@StefH Yes, I can get String.Split to work on its own, but when pairing the result with #each to iterate over the array, I get the weird behavior shown above, showing the length of the array.

StefH commented 1 year ago

Currently, the result from that string.split is a json-array. Not a real object array which can be used in each-loop.

I can't remember why I build this code to behave like this... Maybe a different usecase....

I need to think on this...

kspearrin commented 1 year ago

In the meantime, I was able to fix this by just overloading the exiting String.Split helper with my own custom one:

            context.RegisterHelper("String.Split", (context, arguments) =>
            {
                var value = arguments[0] as string;
                var separator = arguments[1] as string;
                return value.Split(separator);
            });
StefH commented 1 year ago

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

StefH commented 1 year ago

@kspearrin Preview version 2.3.8-ci-16673. should solve this. Can you test ?

(https://github.com/Handlebars-Net/Handlebars.Net.Helpers/wiki/MyGet)

StefH commented 1 year ago

@kspearrin Can you test this preview version?

(If it's ok, then I can merge PR to master and take a look at your PR)

kspearrin commented 1 year ago

@StefH Confirmed that it works.