Passing more foreach options to snippets

I had started putting foreach loops inside of snippets, then calling the snippet.
I already pass through options like limit(), listed(), and others.
Now I’d like to pass through the shuffle() into the snippet.

I’ve searched around the forum, and have even watched some of the instructions on the latest video from YouTube about filtering (awesome!).

Back to snippets, here’s the best explanation I can find, but doesn’t go so far as describing how to do my ask:

Knowing that where it’s placed in the chain is important, how do I add a shuffle option that can be turned on when calling a foreach snippet?

Could you please provide an example, I think I’m missing some context here.



<?php snippet('np-list', [
    'limit' => '3',
    'offset' => '0',
    'shuffle' => '',


    $limit = $limit ?? '3';
    $offset = $offset ?? '0';
    $sort = $sort ?? 'asc'; 
    $shuffle =

<?php foreach (page('nps')
    ->sortBy('num', $sort)
    ->offset($offset) as $np) : ?>

Yeah, not sure how to approach the shuffle() part which could be an optional “on/off” option.

Well, one way would be to pass true/false as values of `shuffle, the use an if statement. Shuffle doesn’t accept a parameter that you could turn on or off.

The alternative would be a custom pages method that conditionally returns the shuffled collection or the unshuffled collection.

This bit is in the wrong position, you cannot sort the page, so it should be after children, and $page('nps') is wrong code. should be without the $ sign. And single quotes around num are also missing.

1 Like

Thanks, Sonja!
Yes, those typos are me recreating my templates incorrectly here with all of the forum spaces. :wink:

I’ll look into true/false options for this.