Using different loop methods in universal snippet

Hi all - Probably quite a basic templating question, but hoping someone could help me with the best approach. I have a snippet which I want to use as a card layout for projects in a variety of situations (past projects, projects in particular places etc) and therefore need to change the method for each type. So I’l l want to set the forloop in the template file and loop the snippet, but if I define the method in the template then this isn’t passed to the snippet.

For example:

<?php $projects = $pages->listed()->filterby('intendedTemplate', 'project');
  foreach ($projects as $project): ?>
<?php snippet('projectcard') ?>
<?php endforeach ?>

$project will now be unrecognised in the looped snippet.

If i look at the demokit demo theme in github, and specifically the ‘agency’ version, I see the following line of code in the agency-projects.php template:

<?php snippet('agency/projects', ['projects' => collection('agency/projects')]) ?>

Is this an answer to my question? If so I can’t find any documentation on what this collection feature does, but the relevant snippet is declaring the $project variable without any other definition that I can find so I’m wondering if this is it.

Help appreciated as always, thanks!

Yes. But you don’t need a collection, you can pass your $projects variable.

Thanks so much @texnixe - I watched the related collection video and realised any variable passed in the snippet helper overrides the controller; I thought it was the other way round.

Case closed!