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.

Revisions

  1. @elidupuis elidupuis revised this gist Jan 17, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions handlebars-helpers.js
    Original file line number Diff line number Diff line change
    @@ -50,8 +50,8 @@ Handlebars.registerHelper('slice', function(context, block) {


    // return a comma-serperated list from an iterable object
    // usage: {{#toSentance tags}}{{name}}{{/toSentance}}
    Handlebars.registerHelper('toSentance', function(context, block) {
    // usage: {{#toSentence tags}}{{name}}{{/toSentence}}
    Handlebars.registerHelper('toSentence', function(context, block) {
    var ret = "";
    for(var i=0, j=context.length; i<j; i++) {
    ret = ret + block(context[i]);
  2. @elidupuis elidupuis revised this gist Jun 26, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion handlebars-helpers.js
    Original file line number Diff line number Diff line change
    @@ -70,7 +70,7 @@ Handlebars.registerHelper('toSentance', function(context, block) {
    // usage: {{dateFormat creation_date format="MMMM YYYY"}}
    Handlebars.registerHelper('dateFormat', function(context, block) {
    if (window.moment) {
    var f = block.hash.format || "MMM Mo, YYYY";
    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.
  3. @elidupuis elidupuis revised this gist Dec 19, 2011. 1 changed file with 7 additions and 5 deletions.
    12 changes: 7 additions & 5 deletions handlebars-helpers.js
    Original file line number Diff line number Diff line change
    @@ -26,12 +26,13 @@ Handlebars.registerHelper('first', function(context, block) {



    // a limited 'each' loop.
    // usage: {{#limit items offset="1" limit="5"}} : items 1 thru 6
    // usage: {{#limit items limit="10"}} : items 0 thru 9
    // usage: {{#limit items offset="3"}} : items 3 thru context.length
    // a iterate over a specific portion of a list.
    // usage: {{#slice items offset="1" limit="5"}}{{name}}{{/slice}} : items 1 thru 6
    // usage: {{#slice items limit="10"}}{{name}}{{/slice}} : items 0 thru 9
    // usage: {{#slice items offset="3"}}{{name}}{{/slice}} : items 3 thru context.length
    // defaults are offset=0, limit=5
    Handlebars.registerHelper('limit', function(context, block) {
    // todo: combine parameters into single string like python or ruby slice ("start:length" or "start,length")
    Handlebars.registerHelper('slice', function(context, block) {
    var ret = "",
    offset = parseInt(block.hash.offset) || 0,
    limit = parseInt(block.hash.limit) || 5,
    @@ -47,6 +48,7 @@ Handlebars.registerHelper('limit', function(context, block) {




    // return a comma-serperated list from an iterable object
    // usage: {{#toSentance tags}}{{name}}{{/toSentance}}
    Handlebars.registerHelper('toSentance', function(context, block) {
  4. @elidupuis elidupuis revised this gist Dec 19, 2011. 1 changed file with 24 additions and 0 deletions.
    24 changes: 24 additions & 0 deletions handlebars-helpers.js
    Original file line number Diff line number Diff line change
    @@ -24,6 +24,29 @@ Handlebars.registerHelper('first', function(context, block) {
    return block(context[0]);
    });



    // a limited 'each' loop.
    // usage: {{#limit items offset="1" limit="5"}} : items 1 thru 6
    // usage: {{#limit items limit="10"}} : items 0 thru 9
    // usage: {{#limit items offset="3"}} : items 3 thru context.length
    // defaults are offset=0, limit=5
    Handlebars.registerHelper('limit', function(context, block) {
    var ret = "",
    offset = parseInt(block.hash.offset) || 0,
    limit = parseInt(block.hash.limit) || 5,
    i = (offset < context.length) ? offset : 0,
    j = ((limit + offset) < context.length) ? (limit + offset) : context.length;

    for(i,j; i<j; i++) {
    ret += block(context[i]);
    }

    return ret;
    });



    // return a comma-serperated list from an iterable object
    // usage: {{#toSentance tags}}{{name}}{{/toSentance}}
    Handlebars.registerHelper('toSentance', function(context, block) {
    @@ -38,6 +61,7 @@ Handlebars.registerHelper('toSentance', function(context, block) {
    });



    // format an ISO date using Moment.js
    // http://momentjs.com/
    // moment syntax example: moment(Date("2011-07-18T15:50:52")).format("MMMM YYYY")
  5. @elidupuis elidupuis revised this gist Dec 16, 2011. 1 changed file with 16 additions and 0 deletions.
    16 changes: 16 additions & 0 deletions handlebars-helpers.js
    Original file line number Diff line number Diff line change
    @@ -2,6 +2,22 @@
    Handlebars helpers
    *******************************/

    // debug helper
    // usage: {{debug}} or {{debug someValue}}
    // from: @commondream (http://thinkvitamin.com/code/handlebars-js-part-3-tips-and-tricks/)
    Handlebars.registerHelper("debug", function(optionalValue) {
    console.log("Current Context");
    console.log("====================");
    console.log(this);

    if (optionalValue) {
    console.log("Value");
    console.log("====================");
    console.log(optionalValue);
    }
    });


    // return the first item of a list only
    // usage: {{#first items}}{{name}}{{/first}}
    Handlebars.registerHelper('first', function(context, block) {
  6. @elidupuis elidupuis revised this gist Dec 12, 2011. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions handlebars-helpers.js
    Original file line number Diff line number Diff line change
    @@ -3,11 +3,13 @@
    *******************************/

    // return the first item of a list only
    // usage: {{#first items}}{{name}}{{/first}}
    Handlebars.registerHelper('first', function(context, block) {
    return block(context[0]);
    });

    // return a comma-serperated list from an iterable object
    // usage: {{#toSentance tags}}{{name}}{{/toSentance}}
    Handlebars.registerHelper('toSentance', function(context, block) {
    var ret = "";
    for(var i=0, j=context.length; i<j; i++) {
    @@ -23,6 +25,7 @@ Handlebars.registerHelper('toSentance', function(context, block) {
    // format an ISO date using Moment.js
    // http://momentjs.com/
    // moment syntax example: moment(Date("2011-07-18T15:50:52")).format("MMMM YYYY")
    // usage: {{dateFormat creation_date format="MMMM YYYY"}}
    Handlebars.registerHelper('dateFormat', function(context, block) {
    if (window.moment) {
    var f = block.hash.format || "MMM Mo, YYYY";
  7. @elidupuis elidupuis created this gist Dec 12, 2011.
    33 changes: 33 additions & 0 deletions handlebars-helpers.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,33 @@
    /*! ******************************
    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.
    };
    });