Quote from: Bruno17 at Jun 19, 2011, 01:55 PM
I use this snippet.
named it ’ajaxSwitch’
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.
I made a little changes and add it as a plugin hooked on "OnWebPagePrerender" to change the output.
/**
* Switches templates on the fly by triggering a get parameter
*/
$properties =& $scriptProperties;
$properties['ajaxid'] = isset($_REQUEST['ajaxid']) ? $_REQUEST['ajaxid'] : false;
$properties['ajaxChunk'] = isset($_REQUEST['ajaxChunk']) ? $_REQUEST['ajaxChunk'] : 'ajaxChunk';
if(!empty($properties['ajaxid'])) {
$output = '';
$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'])) {
$output = $modx->getChunk($properties['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'];
}
echo $output;
die();
}
This checks the ?ajaxid=x&ajaxChunk=chunkName in the URL.. so could every page being loaded as ajax or in colorbox frame!
The only note I found; some snippet calls like formit will not be processed (??)