Kirby Modules: pass data

How would you use the below function? In which format do you put data inside data = []?

$page->modules($data = [], $return = false);

I tried $data = $collection and $data = [$collection] and it seems not be working.

In my case I need to number each module and need to pass on the collection to the snippet that builds each module.

Thanks!

Use it like this:

$page->modules([
  'collection' => $collection,
]);

you can then use the $collection variable in your template.

D’oh, perfect! Thank you.

Coming back to this question some years later…

I am trying to pass a variable when using Kirby Modules but having no luck.

site.php controller is:

<?php

return function ($page, $pages, $site, $kirby) {
    $example = 'duck';

    return compact(
        'example'
    );
};

and in home.php template I have:

<?= $page->modules([
  'example' => $example,
]) ?>

This however isn’t working for me. :frowning: