Skip to content

Instantly share code, notes, and snippets.

@biilmann
Created March 18, 2013 21:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save biilmann/5190820 to your computer and use it in GitHub Desktop.
Save biilmann/5190820 to your computer and use it in GitHub Desktop.
Random shuffle for image galleries
<!-- Content with image gallery field called "image_gallery" -->
<pop:content>
<pop:random:gallery field="image_gallery">
<pop:value resize="fill" width="200"/>
<pop:random:gallery>
</pop:content>
var shuffle = function(images) {
var a = [images[0]];
var i = 1;
while (images[i]) {
var j = Math.floor(Math.random() * (i + 1));
a[i] = a[j];
a[j] = images[i];
i++;
}
return a;
};
exports.gallery = function(options, _, scope) {
var gallery = scope[options.field].images;
return gallery.length ? shuffle(gallery) : [];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment