Reading external JSON


#21

@texnixe Thanks that was useful.

I have now solved this whole problem, after 3 days of swearing :slight_smile:

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?


#22

Here’s how: https://getkirby.com/docs/templates/snippets/#passing-variables-to-snippets

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.


#23

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 :slight_smile:

This works: <?php require_once 'site/snippets/site-caldata.php'; ?>

What does not work is this:

<?php snippet('site-caldata.php') ?>
<?php snippet('site-calendar', ['eventroot' => $kcaleventlist]) ?>

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.


#24

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.


#25

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.


#26

I’d probably wrap that stuff in plugin functions that return the variables so you can easily call those or pass them into your site-calendar snippet.


#27

I’ll look into that, as its something I will need on other websites. If it comes out ok ill release to the community as a plugin.


#28

inside snippet:
you could encode the return value of the snippet as json.

when calling snippet:
then decode that string with asociative flag.

but that takes up cpu time and a plugin/global function will be faster.


#29

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.