I use this snippet.
named it ’ajaxSwitch’
<?php
$output = '';
$properties =& $scriptProperties;
$properties['ajaxid'] = isset($_REQUEST['ajaxid']) ? $_REQUEST['ajaxid'] : 'none';
$properties['cache'] = isset($cache) ? (boolean) $cache : (boolean) $modx->getOption('cache_resource', $properties, false);
$properties[xPDO::OPT_CACHE_KEY] = $modx->getOption('cache_resource_key', $properties, 'default');
$properties[xPDO::OPT_CACHE_HANDLER] = $modx->getOption('cache_resource_handler', $properties, 'xPDOFileCache');
$properties[xPDO::OPT_CACHE_EXPIRES] = (integer) $modx->getOption(xPDO::OPT_CACHE_EXPIRES, $properties, 0);
if ($properties['cache']) {
$properties['cachePageKey'] = $modx->resource->getCacheKey() . '/' . $properties['ajaxid'] . '/' . md5(implode('', $modx->request->getParameters()));
$properties['cacheOptions'] = array(
xPDO::OPT_CACHE_KEY => $properties[xPDO::OPT_CACHE_KEY],
xPDO::OPT_CACHE_HANDLER => $properties[xPDO::OPT_CACHE_HANDLER],
xPDO::OPT_CACHE_EXPIRES => $properties[xPDO::OPT_CACHE_EXPIRES],
);
}
$cached = false;
if ($properties['cache']) {
if ($modx->getCacheManager()) {
$cached = $modx->cacheManager->get($properties['cachePageKey'], $properties['cacheOptions']);
}
}
if (empty($cached) || !isset($cached['properties']) || !isset($cached['output'])) {
if ($properties['ajaxid'] == 'none'){
$output = $modx->getChunk($defaultChunk);
}else{
$output = $modx->getChunk($ajaxChunk,$_REQUEST);
}
if ($properties['cache'] && $modx->getCacheManager()) {
$cached = array('properties' => $properties, 'output' => $output);
$modx->cacheManager->set($properties['cachePageKey'], $cached, $properties[xPDO::OPT_CACHE_EXPIRES], $properties['cacheOptions']);
}
} else {
$properties = $cached['properties'];
$output = $cached['output'];
}
return $output;
in my template i put something like that:
[[!ajaxSwitch?
&ajaxChunk=`ajaxTemplate`
&defaultChunk=`baseTemplate`
&cache=`1`
]]
than I create two chunks, one for default and one for ajax-calls.
On my ajax-chunk I can place different snippets, which return output for ajaxcalls with different ajaxids.
This way I can call different cached parts of my page by ajax with &ajaxid=`sidebox` or &ajaxid=`slider` in the URL.