Controller not working with snippet

Hi,

I have a showcase snippet inside my projects.php template. When I put the templates logic inside a controller I get an errormessage for undefined variables.
Those variables are actually used inside the snippet.

My question is, if snippets have access to the controller of their parent template?

thanks in advance!

The variables returned by a page controller should be available for its snippets.

Can you share your controller code ?

Have you tried to use them within the template directly (logic still in controller but try to echo the variable within the template) to see if they’re also undefined there ?

You probably have to pass the variables to the snippet.

I also assumed so but tried it with multiple setups / variable types and they seem to always be accessible in snippets without passing them explicitely, which led me to believe it’s probably an issue while returning the variables :thinking:

<?php return function($site, $pages, $page) {

// fetch the basic set of pages
$projects = $page->children()->visible();

// add the tag filter
if($tag = param('tag')) {
$projects = $projects->filterBy('tags', $tag, ',');
}

// fetch all tags
$tags = $projects->pluck('tags', ',', false);


return compact('projects', 'tags', 'tag');

};

?>

I changed the controllers name to site.php and everything worked out as expected.
I renamed it again to projects.php and now it works… I don’t know why :confused:

Nothing to do with your issue, but don’t use PHP closing tags at the end of a PHP only file.