Skip to content

Instantly share code, notes, and snippets.

@nathancolgate
Created February 20, 2013 18:52
Show Gist options
  • Save nathancolgate/4998033 to your computer and use it in GitHub Desktop.
Save nathancolgate/4998033 to your computer and use it in GitHub Desktop.
mustache.js in_groups_of I needed mustache.js to iterate over a list in groups of 2 to take advantage of Twitter Bootstraps “row” and “span” classes. Here is how I got it done (using jQuery).
$.fn.inGroupsOf = function( countPerGroup, groupName ) {
var groups = [], offset = 0, $group, jsonOptions;
while ( ($group = this.slice( offset, (countPerGroup + offset) )).length ) {
jsonOptions = {};
jsonOptions[groupName] = $group.toArray();
groups.push( jsonOptions );
offset += countPerGroup;
}
return groups;
};
var renderEmailImageCategories = function(email_image_categories){
$.each(email_image_categories, function(index, email_image_category) {
email_image_category.image_groups = function() {
return $(this.images).inGroupsOf(2,'images');
};
});
template = $.mustache('{{#email_image_categories}}<h4>{{name}}{{^name}}My Images{{/name}}</h4>{{#image_groups}}<div class="row">{{#images}}{{#.}}<a href="#" class="email_image_link" data- data-url="{{file.span3.url}}"><img src="{{file.span3.url}}" class="span3" /></a>{{/.}}{{/images}}</div>{{/image_groups}}{{/email_image_categories}}',{"email_image_categories": email_image_categories});
$('#custom_email_image_body').html(template);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment