Skip to content

Instantly share code, notes, and snippets.

@nijikokun
Created October 9, 2012 20:29
Show Gist options
  • Save nijikokun/3861219 to your computer and use it in GitHub Desktop.
Save nijikokun/3861219 to your computer and use it in GitHub Desktop.
Handlebars.js Filters for Strings
/**
* Basic Standard Filters for strings:
*
* {{size "Plus"}}
*
* Would give you 4!
*
* @author: Nijiko Yonskai
*/
var strings = {
size: function (input) {
return input ? input.length : 0;
},
downcase: function (input) {
return typeof input === 'string' ? input.toLowerCase() : input;
},
upcase: function (input) {
return typeof input === 'string' ? input.toUpperCase() : input;
},
capitalize: function (input) {
return typeof input === 'string' ? input.charAt(0).toUpperCase() + input.slice(1) : input;
},
escape: function (input) {
return escape(input);
},
slug: function (input) {
if (!input || typeof input !== 'string') return input;
return input.replace(/^\s+|\s+$/g, '').toLowerCase().replace(/[^a-z0-9 -]/g, '').replace(/\s+/g, '-').replace(/-+/g, '-');
}
}
// Require & HBS.js
//for (var i in strings) {
// define('template/helpers/' + i, [ 'Handlebars' ], function (Handlebars) {
// Handlebars.registerHelper(i, strings[i]);
// return strings[i];
// });
//}
// Handlebars.js
for (var i in strings) {
Handlebars.registerHelper(i, strings[i]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment