Skip to content

Instantly share code, notes, and snippets.

@roden0
Forked from elidupuis/handlebars-helpers.js
Created May 9, 2014 09:59
Show Gist options
  • Save roden0/cdee4daa828b2a40e717 to your computer and use it in GitHub Desktop.
Save roden0/cdee4daa828b2a40e717 to your computer and use it in GitHub Desktop.
/*! ******************************
Handlebars helpers
*******************************/
// return the first item of a list only
Handlebars.registerHelper('first', function(context, block) {
return block(context[0]);
});
// return a comma-serperated list from an iterable object
Handlebars.registerHelper('toSentance', function(context, block) {
var ret = "";
for(var i=0, j=context.length; i<j; i++) {
ret = ret + block(context[i]);
if (i<j-1) {
ret = ret + ", ";
};
}
return ret;
});
// format an ISO date using Moment.js
// http://momentjs.com/
// moment syntax example: moment(Date("2011-07-18T15:50:52")).format("MMMM YYYY")
Handlebars.registerHelper('dateFormat', function(context, block) {
if (window.moment) {
var f = block.hash.format || "MMM Mo, YYYY";
return moment(Date(context)).format(f);
}else{
return context; // moment plugin not available. return data as is.
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment