Skip to content

Allow .length in addition to .Count on arrays #307

Closed
dotnet/arcade
#16884

Description

@johnknoop

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.

Activity

  1. rexm commented on Jun 24, 2019

    @rexm
    Member

    Without adding any new concepts to the library, such as aliasing, this is solvable by using a helper, such as {{len myArray}}.

  2. johnknoop commented on Jun 24, 2019

    @johnknoop
    Author

    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.

  3. rexm commented on Jun 24, 2019

    @rexm
    Member

    PR welcome

  4. johnknoop commented on Jun 24, 2019

    @johnknoop
    Author

    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?

  5. rexm commented on Jun 24, 2019

    @rexm
    Member

    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.

  6. added a commit that references this issue on Apr 23, 2020
  7. vandre commented on Mar 5, 2022

    @vandre

    @zjklee it appears .length still does not work if object type is IEnumerable, which is an array-like structure.

  8. oformaniuk commented on Mar 5, 2022

    @oformaniuk
    Member

    Hello @vandre . It does not work because IEnumerable does not have Count concept. If you need it - you may implement your custom alias provider.

  9. vandre commented on Mar 5, 2022

    @vandre

    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:

    Handlebars.RegisterHelper("IsFalsyOrEmpty", (output, context, arguments) =>
    			{
    				bool IsFalsyOrEmpty = HandlebarsUtils.IsFalsyOrEmpty(arguments[0]) ;
    				output.WriteSafeString(IsFalsyOrEmpty ? "true":null);
    			});
    
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