Skip to content

Instantly share code, notes, and snippets.

@hoetmaaiers
Last active September 16, 2017 16:25
Show Gist options
  • Save hoetmaaiers/8145894 to your computer and use it in GitHub Desktop.
Save hoetmaaiers/8145894 to your computer and use it in GitHub Desktop.
Handlebars Helpers

Pluralize or singular string based on a number

Handlebars.registerHelper('pluralize', function(number, singular, plural) {
    if (number === 1) {
        return singular;
    } else {
        return (typeof plural === 'string' ? plural : singular + 's');
    }
});

Handlebars.registerHelper('pluralCount', function(number, singular, plural) {
    return number+' '+Handlebars.helpers.pluralize.apply(this, arguments);
});

Moment.js as a helper method

Handlebars.registerHelper('moment', function(context, block) {
  if (window.moment) {
    var f = block.hash.format || "MMM Do, 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