Add "cutString" Handlebars.js helper

This commit is contained in:
Dmitriy Simushev 2014-09-11 10:09:14 +00:00
parent 75276ab2da
commit 333f769a16
2 changed files with 31 additions and 0 deletions

View File

@ -233,4 +233,17 @@
return result; return result;
}); });
/**
* Registers "cutString" helper.
*
* This helper cuts a string if it exceeds specified length. Example of
* usage:
* <code>
* {{cutString string length}}
* </code>
*/
Handlebars.registerHelper('cutString', function(length, options) {
return options.fn(this).substr(0, length);
});
})(Mibew, Handlebars); })(Mibew, Handlebars);

View File

@ -167,3 +167,21 @@ test('repeat', function() {
'Test repeating' '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'
);
});