Skip to content

Commit

Permalink
Add special @index variable to #repeat helper, closes #67
Browse files Browse the repository at this point in the history
  • Loading branch information
gakimball committed Mar 8, 2017
1 parent a0b47b6 commit 28b3e1f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
13 changes: 12 additions & 1 deletion helpers/repeat.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
var Handlebars = require('handlebars');

/**
* Handlebars block helper that repeats the content inside of it n number of times.
* @param {integer} count - Number of times to repeat.
Expand All @@ -8,9 +10,18 @@
*/
module.exports = function(count, options) {
var str = '';
var data;

if (options.data) {
data = Handlebars.createFrame(options.data);
}

for (var i = 0; i < count; i++) {
str += options.fn(this);
if (data) {
data.index = i;
}

str += options.fn(this, { data: data });
}

return str;
Expand Down
10 changes: 5 additions & 5 deletions test/fixtures/helper-repeat/build/index.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<html>
<body>
<p>Repeat!</p>
<p>Repeat!</p>
<p>Repeat!</p>
<p>Repeat!</p>
<p>Repeat!</p>
<p>Repeat 0!</p>
<p>Repeat 1!</p>
<p>Repeat 2!</p>
<p>Repeat 3!</p>
<p>Repeat 4!</p>

</body>
</html>
10 changes: 5 additions & 5 deletions test/fixtures/helper-repeat/expected/index.html
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<html>
<body>
<p>Repeat!</p>
<p>Repeat!</p>
<p>Repeat!</p>
<p>Repeat!</p>
<p>Repeat!</p>
<p>Repeat 0!</p>
<p>Repeat 1!</p>
<p>Repeat 2!</p>
<p>Repeat 3!</p>
<p>Repeat 4!</p>

</body>
</html>
2 changes: 1 addition & 1 deletion test/fixtures/helper-repeat/pages/index.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{{#repeat 5}}
<p>Repeat!</p>
<p>Repeat {{@index}}!</p>
{{/repeat}}

0 comments on commit 28b3e1f

Please sign in to comment.