This is a follow up question to this one.
I have a function to build a pages collection from a structured field which is like:
$elements = $page->elements()->toStructure();
$parent = $site->index()->filterBy('template', $type)->first()->parent();
$cards = new Pages();
foreach($elements as $element) {
$cards->add($parent->find($element->uid()));
}
How can i use this function in multiple templates and return $cards to the template?