I have now solved this whole problem, after 3 days of swearing
Now i’m cleaning it up a bit, i want to move all my raw PHP to snippet so that i can use it on multiple templates without duplication. How do i do this so that the variables are accessable to the template that i place the snippet in?
From the docks It looks like the true parameter is what I want but it had no effect:
<?php snippet('site-caldata', null, true); ?>
The rest of the page cannot see the variables set in that snippet. Is it possible to do this? Should i use a standard PHP require instead?
Setting the return parameter to true does not help in this context.
It might be useful to define all variables you need in the snippet itself, so that the snippet is an independent unit you can just include anywhere, otherwise you would have to define your variables in each template/controller.
Thanks, but its the other direction i need want to go. I don’t want to pass a variable TO the snippet, the variable is in the snippet with a value and I want to get its value outside of the snippet, but it throws an error.
It works with a php require statement at the top of the template but i wondered if there was a more Kirby way
This works: <?php require_once 'site/snippets/site-caldata.php'; ?>
The $kcaleventlist variable defined and given a value in the first snippet. I wan’t to use its value on the second snippet, but it errors and tells me the variable is undefined.
Ok, I think then it’s better to require the snippet or maybe define the data in a plugin file so that it get’s loaded automatically. I don’t know what is in your snippet, so hard to tell. The snippet() helper does not help in this case.
The snippet is just a lot of raw php that loads Google’s php api library, fetches all events from Kirby into an array, and does the same with the remote google calendar data. I end up with 3 arrays, $kcaleventlist, $gcaleventlist and $combinedeventlist.
It’s these three variables I need to pick up on in multiple templates.
Theres no Kirby code in the snippet, its just all vanilla PHP.
EDIT: Actually thats not true, theres like 3 lines of Kirby code in the snippet.
Gosh, thanks @bnomei but i gave up on turning it into a plugin a year ago (beyond my PHP skills). If your up for the task feel free to take it over, theres a working repo up here on github. Id love to see it cleaned up and refactored into a plugin.