Skip to content

Instantly share code, notes, and snippets.

@amitkumar
Last active March 24, 2022 16:32
Show Gist options
  • Select an option

  • Save amitkumar/b1969e86e1379db83511 to your computer and use it in GitHub Desktop.

Select an option

Save amitkumar/b1969e86e1379db83511 to your computer and use it in GitHub Desktop.

Revisions

  1. amitkumar revised this gist Jul 30, 2014. 2 changed files with 29 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions handlebars-helpers-templates.html
    Original file line number Diff line number Diff line change
    @@ -12,3 +12,10 @@
    <span class="first-friend-name">{{#array_item friends 0}}{{firstName}} {{lastName}}{{/array_item}}</span>
    </script>

    <script id="each_upto_example" type="text/x-handlebars-template">
    <ul>
    {{#each_upto friends 3}}
    <li class="item item-{{@itemIndex}}"></li>
    {{/each_upto}}
    </ul>
    </script>
    22 changes: 22 additions & 0 deletions handlebars-helpers.js
    Original file line number Diff line number Diff line change
    @@ -8,4 +8,26 @@ Handlebars.registerHelper('if_equals', function(conditional, value, options) {

    Handlebars.registerHelper('array_item', function(array, index, options) {
    return options.fn(array[index]);
    });

    /**
    * @param ary {Array}
    * @param max {Number} The max number of items to display from the array
    * @param [options.skip] {Number=0} Optional. Number of items to skip in the array
    */
    Handlebars.registerHelper('each_upto', function(ary, max, options) {
    if(!ary || ary.length == 0)
    return options.inverse(this);

    var result = [],
    skip = (options.hash ? (options.hash.skip ? options.hash.skip : 0) : 0),
    i = skip;

    max += skip;

    for(; i < max && i < ary.length; ++i) {
    result.push(options.fn(ary[i], { data : { itemIndex : i } } ));
    }

    return result.join('');
    });
  2. amitkumar created this gist Jul 30, 2014.
    14 changes: 14 additions & 0 deletions handlebars-helpers-templates.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    <script id="if_equals_example" type="text/x-handlebars-template">
    {{#if_equals objectType 'message'}}
    <div class="message">
    Message
    </div>
    {{else}}

    {{/if_equals}}
    </script>

    <script id="array_item_example" type="text/x-handlebars-template">
    <span class="first-friend-name">{{#array_item friends 0}}{{firstName}} {{lastName}}{{/array_item}}</span>
    </script>

    11 changes: 11 additions & 0 deletions handlebars-helpers.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    Handlebars.registerHelper('if_equals', function(conditional, value, options) {
    if (conditional === value){
    return options.fn(this);
    } else {
    return options.inverse(this);
    }
    });

    Handlebars.registerHelper('array_item', function(array, index, options) {
    return options.fn(array[index]);
    });