From 75276ab2dab63bf2691f5b9147850cbd9022ed4c Mon Sep 17 00:00:00 2001 From: Dmitriy Simushev Date: Thu, 11 Sep 2014 09:45:23 +0000 Subject: [PATCH] Add "repeat" Handlebars.js helper --- .../js/source/default/handlebars_helpers.js | 19 +++++++++++++++++++ .../test_cases/handlebars_helpers_tests.js | 12 ++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/mibew/js/source/default/handlebars_helpers.js b/src/mibew/js/source/default/handlebars_helpers.js index 3e85109c..f335d28b 100644 --- a/src/mibew/js/source/default/handlebars_helpers.js +++ b/src/mibew/js/source/default/handlebars_helpers.js @@ -214,4 +214,23 @@ return options.inverse(this); } }); + + /** + * Registers "repeat" helper. + * + * This helper repeats a string specified number of times. Example of usage: + * + * {{#repeat times}}content to repeat{{/repeat}} + * + */ + 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); \ No newline at end of file diff --git a/src/tests/client_side/qunit/test_cases/handlebars_helpers_tests.js b/src/tests/client_side/qunit/test_cases/handlebars_helpers_tests.js index 2cac3877..7b262f66 100644 --- a/src/tests/client_side/qunit/test_cases/handlebars_helpers_tests.js +++ b/src/tests/client_side/qunit/test_cases/handlebars_helpers_tests.js @@ -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' + ); +});