question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
Thanoodcommented, Sep 7, 2016

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;
});
0reactions
oformaniukcommented, Sep 7, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Handlebars: How pass @index as parameter to custom ...
I'm using Handelbars.PHP, but maybe a solution with Handlebars. · A custom helper content that expects two parameters a collection and an index....
Read more >
How pass @index as parameter to custom helper · Issue #27
Setup: - A custom helper content that expects two parameters a collection and an index. - An #each helper invocation that calls to...
Read more >
hapi — Create and Use Custom Handlebars Helpers
This time, we use the helpersPath option and create our own custom handlebars helpers for views rendered by hapi.
Read more >
Action View Form Helpers
How to make model-centric forms for creating and editing specific database records ... Basic Structures; Combining Them; The fields_for Helper :index Option.
Read more >
Handlebars helper reference
Custom helpers, Block helpers, Data variables ... d - Generate indices for substring matches. g - Global search. i - Case-insensitive search.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found