Skip to content

Instantly share code, notes, and snippets.

@greggb
Forked from there4/handlebars-helpers.js
Last active December 10, 2015 00:28
Show Gist options
  • Save greggb/4351083 to your computer and use it in GitHub Desktop.
Save greggb/4351083 to your computer and use it in GitHub Desktop.
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;
});
//with just 's' when used with a numerical count
// usage: {{pluralize {{tag Name Containing count}} "item"}}
Handlebars.registerHelper('pluralize', function (context, word) {
return (context === 1) ? word : word + 's';
});
// 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