From 333f769a166563c0368970a5c436399e02dfc755 Mon Sep 17 00:00:00 2001 From: Dmitriy Simushev Date: Thu, 11 Sep 2014 10:09:14 +0000 Subject: [PATCH] Add "cutString" Handlebars.js helper --- .../js/source/default/handlebars_helpers.js | 13 +++++++++++++ .../test_cases/handlebars_helpers_tests.js | 18 ++++++++++++++++++ 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 f335d28b..e06fc7ca 100644 --- a/src/mibew/js/source/default/handlebars_helpers.js +++ b/src/mibew/js/source/default/handlebars_helpers.js @@ -233,4 +233,17 @@ return result; }); + + /** + * Registers "cutString" helper. + * + * This helper cuts a string if it exceeds specified length. Example of + * usage: + * + * {{cutString string length}} + * + */ + Handlebars.registerHelper('cutString', function(length, options) { + return options.fn(this).substr(0, length); + }); })(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 7b262f66..2a783f40 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 @@ -167,3 +167,21 @@ test('repeat', function() { 'Test repeating' ); }); + +// Test "cutString" Handlebars helper +test('cutString', function() { + var template = '{{#cutString length}}{{str}}{{/cutString}}'; + var compiledTemplate = Handlebars.compile(template); + + equal( + compiledTemplate({str: 'Hello world!', length: 40}), + 'Hello world!', + 'Test cutting of a string that is shorter than specified length' + ); + + equal( + compiledTemplate({str: 'Hello world!', length: 5}), + 'Hello', + 'Test cutting of a string that is longer than specified length' + ); +});