Is there a way to link up a snippet to a controller

Hello All

I know that it is possible to link templates with controllers of the same name whereby /site/templates/sometemplate.php will accept arguments returned by /site/controllers/sometemplate.php. Is there a way to do this for snippets where /site/snippets/somesnippet.php will accept arguments returned by site/controllers/somesnippet.php?

If the answer is yes, how is this done. If the answer is no, I assume that I will have embed the arguments that I want the snippet to accept into the controller file called by the template that I am inserting the snippet into? How is this done?

Here is why I am asking this. In the documentation page under the Solutions category, there are a few steps on how to create a one-pager website. My code for my home.php template is exactly as it is layed out in that example. Namely:

<?php

snippet('header');

foreach($pages->visible() as $section) {
  snippet($section->uid(), array('data' => $section));
}

snippet('footer');

?>

One of the snippets being called by this foreach loop is /site/snippets/banner.php, which contains an image file that will be used as a background image in my banner. Using the GD Library, I want to be able write a function to crop and/or resize this image and reduce it’s file size (if necessary). So as to separate logic from form, it would not be desirable to have this code within my home.php template or my banner.php snippet, for that matter. Thus the question about snippets.

I’m sure I could think up a solution eventually, but I’m just asking in case someone has an elegant solution already!

I would be mighty happy if someone could help me with this.

Regards,
Paul B.

For the one pager, you can return your variables in the home.php controller, they are then accessible in the snippets that you call in that loop in your home.php template.

@texnixe - I will try this out in the coming days and let you know of my progress. This is my first foray into the world of controllers so I though I should ask this before I start.

Many Thanks!

Paul

1 Like

You’re welcome :smile: Don’t hesitate to ask if you encounter any problems …

OK. I couldn’t wait to make sure that this worked. And it worked. You can see the banner image on the site I’m building using the Materialize framework.

Again. Many thanks!

Paul