This is Kirby 2, and I need to use params() on a site controller that passes a variable to a snippet that is loaded via ajax.
I explain:
I do a jquery $.post call agains a router in config. The router finds the right snippet and returns it encoded, then I add it to the page via jquery .html()
JS:
$.post(
baseurl + "/eco",
{
snippet: snippet,
page: page
}, function(data){
$(container).html(data);
}, "json");
Route:
c::set('routes', array(
array(
'pattern' => 'eco',
'method' => 'POST',
'action' => function() {
$dasSnippet = get('snippet');
$dasPage = get('page');
try {
return json_encode(snippet($dasSnippet, array('p' => $dasPage), true));
} catch (Exception $ex) {
return $ex->getMessage();
}
}
)
));
There is a site controller that defines and returns variables that are used in the loaded snippets. And this site controller uses param() to conditionally define certain variables. When the snippet is loaded via ajax, from my tests it seems all variables coming from the controller are available to the ajax loaded snippet, except the one that depends on param().
In other words, param() does not seem to work when ajax loading the snippet in this manner.
This is the site controller:
<?php
return function($site, $pages, $page) {
$latestEdition = page('editions')->children()->visible()->first();
/* If there is an archive parameter in the URL use it to set currentEdition, otherwise current is latest*/
if (param('archive')) {
$currentEdition = page('editions')->children()->find(param('archive'));
$archive = true;
} else {
$currentEdition = $latestEdition;
}
$currentThemeText = $currentEdition->find('theme-text');
$currentExhibitions = $currentEdition->find('exhibitions');
$currentEvents = $currentEdition->find('events');
$currentAssProgram = $currentEdition->find('associates-program');
$currentWriter = $currentEdition->find('writer-in-residency');
// pass variables to the template
return compact(
'currentEdition',
'currentThemeText',
'latestEdition',
'currentExhibitions',
'currentEvents',
'currentAssProgram',
'currentWriter',
'archive'
);
};
Are my expectations here correct ? am I doing something wrong?