-
-
Save there4/2903216 to your computer and use it in GitHub Desktop.
Simple Handlebars.js helpers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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