Snippet pass all data to child

in index.php

$my_data['a'] = "some a"; 
$my_data['z'] = "some z"; // Lot of stuff
return page('tpl-list')->render($my_data);

in tpl-list I want to pass through the whole big $my_data to the child snippet…

<?php snippet('a-partial', ... )?>

How do I manage that?

Use a controller to build up the data. You can pass that on to the snippet in the template.

1 Like

It would be helpful to see in what context you are generating the data array. What is index.php?

1 Like

i have an old Kirby2 plugin here, which I want to migrate to Kirby 4. The index.php is the entry point for the plugin, and holds - among other things - all the routes like so:

<?php

Kirby::plugin('my/plugin', [
        [
            'pattern' => URL_BASE . 'list/(:any)',
            'method' => 'GET',
            'action' => function ($filter) {
                include (PATH_ROUTES . 'get-list');
                return page('tpl-list')->render($data);
            } ,
        ],
]);

get-list.php (a sort of controller) creates the data (so far) in a global scope like so:

<?php

$data['field_a'] = 'field a';
$data['field_z'] = 'field z';

tpl-list.php uses the data:

<!-- This is fine -->
<p><?=$field_a;</p>

<!-- this seems impractical with many parameters -->
<?php snippet('partial', ['field_a' => $field_a, 'field_z' => $field_z])?>
<!-- What is the best way to pass all parameters from $data here? -->

Many thanks for your support.

With a little trick, this should work:

 [
            'pattern' => URL_BASE . 'list/(:any)',
            'method' => 'GET',
            'action' => function ($filter) {
                include (PATH_ROUTES . 'get-list');
                $data = ['data' => $data];

                return page('tpl-list')->render($data);
            } ,
        ],

The in your controller:

return function ($page, $data) {
	return [
		'data'   => $data,
	];
	
};

Like that you have access to the complete array and can pass it to the snippet.

I am now renovating the code so that I use Kirby Controller.