Add "repeat" Handlebars.js helper

This commit is contained in:
Dmitriy Simushev 2014-09-11 09:45:23 +00:00
parent 814869ec77
commit 75276ab2da
2 changed files with 31 additions and 0 deletions

View File

@ -214,4 +214,23 @@
return options.inverse(this);
}
});
/**
* Registers "repeat" helper.
*
* This helper repeats a string specified number of times. Example of usage:
* <code>
* {{#repeat times}}content to repeat{{/repeat}}
* </code>
*/
Handlebars.registerHelper('repeat', function(count, options) {
var result = '',
content = options.fn(this);
for (var i = 0; i < count; i++) {
result += content;
}
return result;
});
})(Mibew, Handlebars);

View File

@ -155,3 +155,15 @@ test('ifEqual', function() {
'Test equal values'
);
});
// Test "repeat" Handlebars helper
test('repeat', function() {
var template = '{{#repeat times}}{{foo}}{{/repeat}}';
var compiledTemplate = Handlebars.compile(template);
equal(
compiledTemplate({foo: 'Foo.', times: 3}),
'Foo.Foo.Foo.',
'Test repeating'
);
});