I do ajax-style loading of snippets using a route, as such:
'routes' => [
[
'pattern' => '(:all)echo',
'method' => 'post',
'action' => function () {
$snippet = get('snippet');
return json_encode(snippet($snippet, [], true));
}
],
Then I fetch that and use it to replace innerHTML of a certain div on the page, with something like:
fetch(url, {
method: 'post',
body: JSON.stringify({snippet: snippet}),
headers: {'Content-type': 'application/json '}
}).then(function (response) {
return response.json();
}).then(function (data) {
document.querySelector(container).innerHTML = data;
Which works well, but some returned snippets use collections that are defined in site.php
controller, such as these collections:
$allExhibitions = page('exhibitions')->children()->sortBy('openingdate', 'desc');
$upcomingExhibitions = $allExhibitions->filterBy('openingdate', 'date >', 'today');
$currentExhibitions = $allExhibitions->filterBy('openingdate', 'date <=', 'today')->filterBy('closingdate', 'date >=', 'today');
$pastExhibitions = $allExhibitions->filterBy('closingdate', 'date <', 'today');
What would be a good way to pass these collections to the snippet when returning it from config, or for the snippet to access these collections.
In the past I reassigned needed collections directly on the config page, but in this case that is not necessary, and I would be assigning them twice, on controller and on config.
Thank you