Skip to content

Question: How to create a custom helper with access to @index #151

Closed
dotnet/arcade
#16884

Description

@mattbrailsford

Hi, I want to create a range helper such that I can do the following

{{#range 1 10}}
    <h1>{{@index}}</h1>
{{/range}}

but I can't figure out how you create a custom helper with access to the @index parameter, can anyone point me in the right direction?

Activity

  1. rexm commented on Sep 7, 2016

    @rexm
    Member

    That's not supported right now. Does the JS library support it, and if so how does that work?

  2. Thanood commented on Sep 7, 2016

    @Thanood

    It's shown as a helper example here: http://handlebarsjs.com/block_helpers.html (section "Hash Arguments"), last example.

    {{#list array}}
      {{@index}}. {{title}}
    {{/list}}
    Handlebars.registerHelper('list', function(context, options) {
      var out = "<ul>", data;
    
      if (options.data) {
        data = Handlebars.createFrame(options.data);
      }
    
      for (var i=0; i<context.length; i++) {
        if (data) {
          data.index = i;
        }
    
        out += "<li>" + options.fn(context[i], { data: data }) + "</li>";
      }
    
      out += "</ul>";
      return out;
    });
  3. greg-ellis commented on Dec 2, 2016

    @greg-ellis

    Is this something that's on the roadmap? I was about to switch from our current templating engine to this, but I'm not sure I can. I need to be able to create a two-column or a three-column table (for HTML email) from a list of objects. Without being able to pass @index to some helper to check if the index is a multiple of 3 or whatever, and without adding some sort of index field to whatever data is being passed in, is there any way to do that?

  4. rexm commented on Dec 2, 2016

    @rexm
    Member

    If it's something the handlebarsjs library supports, then absolutely it is in the roadmap. The #1 goal of this library is to stay as close as possible to feature parity with handlebarsjs.

    PRs are always welcome... scratching your own itch is the fastest way to move the project forward. The ability for a helper to add context variables seems like something that will require a bit more thoughtfulness... I look forward to ideas on how to implement it.

  5. added a commit that references this issue on Apr 23, 2020
  6. spc-89 commented on Aug 20, 2020

    @spc-89

    Hello everybody. Please provide some info on the current thread. I saw that some code for this issue was merged into the main branch but I am unable to pass the {{@index}} into a custom Block Helper. It is always sending NULL.

  7. oformaniuk commented on Sep 7, 2020

    @oformaniuk
    Member

    Hello @spc-89 . The functionality is going to become available in 2.0 version of the library. It's going to be published once my fork would be merged to the trunk. Follow PRs to be updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions