mirror of
https://github.com/Mibew/mibew.git
synced 2025-02-07 16:24:43 +03:00
Add "cutString" Handlebars.js helper
This commit is contained in:
parent
75276ab2da
commit
333f769a16
@ -233,4 +233,17 @@
|
||||
|
||||
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);
|
@ -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'
|
||||
);
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user