Created
April 10, 2014 12:14
-
-
Save RobinThrift/10375204 to your computer and use it in GitHub Desktop.
A Handlebar.js helper that returns a given number of items in an array starting from a given index
This file contains hidden or 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
Handlebars.registerHelper('limit', function(collection, limit, start) { | |
var out = [], | |
i, c; | |
start = start || 0; | |
for (i = c = 0; i < collection.length; i++) { | |
if (i >= start && c < limit+1) { | |
out.push(collection[i]); | |
c++; | |
} | |
} | |
return out; | |
}); |
In this case I think that we need to do this.
Handlebars.registerHelper('limit', function( collection, limit, start ) {
return collection.slice( start, limit + 1 );
});
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Eh... is there any reason why you didn't just do this:
Also there is a bug in current implementation, line 8: If you are starting count (c) from 0 you don't need to have
c < limit + 1
: remove the+1