Question

I'm trying to write a Handlebars helper that lets me display an element x number of times, where x is defined in the data passed to the template.

I'm adapting the code I found here for #times. But instead of it running a constant number of times for each template, I want to be able to control that with the data I pass to the template.

This is a contrived example, but if I passed in {stars: 50} to this template, I'd want it to print 50 stars.

{{#times {{stars}} }}★{{/times}} 

This obviously isn't the right way to do this as I get a parse error when compiling the template.

Expecting 'CLOSE', 'CLOSE_UNESCAPED', 'STRING', 'INTEGER', 'BOOLEAN', 'ID', 'DATA', 'SEP', got 'OPEN'

But how do I let my helpers accept dynamic input?

Was it helpful?

Solution

Well I feel silly. You can just use the variables directly without the brackets. So the proper way to do it is like this:

{{#times stars}}★{{/times}}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top