lukaskleinschmidt / kirby-snippet-controller

Define snippet controllers in a similar way to page controllers
MIT License
21 stars 0 forks source link

$data returns everything with $data #1

Closed kevinvennitti closed 1 year ago

kevinvennitti commented 1 year ago

Hi!

I am using a snippet controller to fetch snippet data and get mydata:

snippet('mysnippet', ['mydata' => 'my value']);
// mysnippet.controller.php

<?php

return function ($site, $data) {
  print_r($data);

  return [];
};

$data should be:

Array ( 
  'mydata' => 'my value'
)

Instead, $data returns:

Array ( 
  'kirby' => Kirby\Cms\App Object ( … ), 
  'site' => Kirby\Cms\Site Object ( … ),
  'pages' => Kirby\Cms\Pages Object ( … ),
  'page' => Kirby\Cms\Page Object ( … ),
  'mydata' => 'my value',
)

Am I using it wrong? :)

lukaskleinschmidt commented 1 year ago

The docs are probably not as clear on this part. You get all variables that will be available in your snippet. Actually this is nothing I added specifically. If you would replace the snippets component yourself you would always get at least the kirby, site, pages and page objects at this point as those are the default variables that are always available in snippets.

Filtering out these variables also makes no sense (at least for the plugin) since you can override any of these default variables as well.

snippet('mysnippet', ['page' => null]);
kevinvennitti commented 1 year ago

That's clear, thanks!

(and thanks for the plugin!)

lukaskleinschmidt commented 1 year ago

Actually you issue got me thinking. @kevinvennitti what is your actual use case for the $data array? Do you use it to actually do a loop over it or do you just need one or more values from the array?

I think it would actually make more sense to pass the $data as possible args so you could do something like this. This would also help to properly typehint arguments:

<?php

return function (Site $site, mixed $mydata = null, TargetPage $target)
{
    return [
        //
    ];
};