You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Allow .length in addition to .Count on arrays #307
If the context data has a property called myArray of type Array, this expressions renders fine using the javascript version of Handlebars:
{{myArray.length}}
However, when I compile/render with Handlebars.Net, I have to change the expression to {{myArray.Count}} to make it work. I get why that is, but it would be nice if our hbs templates would work in the Handlebars-JS as well, since we use JS-based tools for previewing etc.
Sure, but then we would have to add the same helper both in C# and javascript.
I can understand that it would feel a bit weird for the compiler to start looking for the string ".length".
At the same time though.. since Handlebars was originally written in Javascript, that sortof becomes the de-facto standard. So I think it would be a good ambition for this project to be able to render anything that can be considered a valid hbs template.
Realistically we probably won’t prioritize this since the need is easily solved using existing features. Contributors’ time (including my own) is finite so we prioritize addressing things that library consumers can’t do. I think the level of effort to add a new compiler feature is probably a few orders of magnitude greater than two one-line helper functions for projects that need cross-platform interoperability with array length. But if it’s important enough to you, you’ll get support from me and the other contributors as you flesh out a proposed design & architecture.
For my use-case I did not care about the exact count; I simply needed a way to not render a table heading if a collection is empty. A common way to accomplish this In Handlebars.js is to check for myObject.length, since a length of 0 is falsy. In contrast, in the current implementation of Handlebars.net myObject.length is always false for IEnumerable objects (Shouldn't the library throw an exception instead ?)
What I ended up doing is exposing the existing HandlebarsUtils.IsFalsyOrEmpty method:
Activity
rexm commentedon Jun 24, 2019
Without adding any new concepts to the library, such as aliasing, this is solvable by using a helper, such as {{len myArray}}.
johnknoop commentedon Jun 24, 2019
Sure, but then we would have to add the same helper both in C# and javascript.
I can understand that it would feel a bit weird for the compiler to start looking for the string ".length".
At the same time though.. since Handlebars was originally written in Javascript, that sortof becomes the de-facto standard. So I think it would be a good ambition for this project to be able to render anything that can be considered a valid hbs template.
rexm commentedon Jun 24, 2019
PR welcome
johnknoop commentedon Jun 24, 2019
I'd love to but I wouldn't know where to start 😂. Maybe you can tag it "help wanted" and we'll see if someone jumps on it?
rexm commentedon Jun 24, 2019
Realistically we probably won’t prioritize this since the need is easily solved using existing features. Contributors’ time (including my own) is finite so we prioritize addressing things that library consumers can’t do. I think the level of effort to add a new compiler feature is probably a few orders of magnitude greater than two one-line helper functions for projects that need cross-platform interoperability with array length. But if it’s important enough to you, you’ll get support from me and the other contributors as you flesh out a proposed design & architecture.
Extreme refactoring/enhancements
vandre commentedon Mar 5, 2022
@zjklee it appears .length still does not work if object type is IEnumerable, which is an array-like structure.
oformaniuk commentedon Mar 5, 2022
Hello @vandre . It does not work because
IEnumerabledoes not haveCountconcept. If you need it - you may implement your custom alias provider.vandre commentedon Mar 5, 2022
For my use-case I did not care about the exact count; I simply needed a way to not render a table heading if a collection is empty. A common way to accomplish this In Handlebars.js is to check for myObject.length, since a length of 0 is falsy. In contrast, in the current implementation of Handlebars.net myObject.length is always false for IEnumerable objects (Shouldn't the library throw an exception instead ?)
What I ended up doing is exposing the existing HandlebarsUtils.IsFalsyOrEmpty method:
StefH commentedon Mar 5, 2022
@vandre
If you are only interested in Empty, you can also take a look at: https://github.com/Handlebars-Net/Handlebars.Net.Helpers
See
https://github.com/Handlebars-Net/Handlebars.Net.Helpers/blob/master/src/Handlebars.Net.Helpers/Helpers/EnumerableHelpers.cs#L56