Skip to content

Instantly share code, notes, and snippets.

@there4
Forked from elidupuis/handlebars-helpers.js
Created June 10, 2012 00:23
Show Gist options
  • Save there4/2903216 to your computer and use it in GitHub Desktop.
Save there4/2903216 to your computer and use it in GitHub Desktop.
Simple Handlebars.js helpers
define([
"use!underscore",
"use!handlebars",
"moment"
],
function(
_, Handlebars, Moment
) {
// usage: {{toLowerCase someString}}
Handlebars.registerHelper('toLowerCase', function(value) {
return (value && _.isString(value)) ? value.toLowerCase() : '';
});
// usage: {{debug}} or {{debug someValue}}
Handlebars.registerHelper("debug", function(optionalValue, options) {
console.group("Handlebar Debug:");
console.log(this);
if (_.isObject(optionalValue) && _.isObject(optionalValue.hash)) {
// this means that the {{debug}} was called without params
}
else {
console.log(optionalValue);
}
console.groupEnd();
});
// usage: {{pluralize collection.length 'quiz' 'quizzes'}}
Handlebars.registerHelper('pluralize', function(number, single, plural) {
return (number === 1) ? single : plural;
});
// usage: {{fromNow date}}
Handlebars.registerHelper('fromNow', function(date) {
moment = new Moment(date);
return moment.fromNow();
});
});
/* End of file handlebars.helpers.js */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment