Created
September 14, 2011 10:40
-
-
Save anonymous/1216291 to your computer and use it in GitHub Desktop.
Simple Ajax Switch as a snippet for modx Revolution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Simple Ajax Switch Snippet for modx Revolution | |
* | |
* Checks whether the page was requested via Ajax and depending on this outputs different chunks. | |
* Save it as a snippet called simpleAjaxSwitch | |
* | |
* Adapted from here: | |
* http://forums.modx.com/thread/15204/change-template-on-the-fly#dis-post-82222 | |
* Thanks to Bruno! | |
* | |
* Usage in Template: | |
* [[!simpleAjaxSwitch? | |
* &ajaxChunk=`ajaxTemplate` | |
* &defaultChunk=`baseTemplate` | |
* &cache=`1` | |
* ]] | |
*/ | |
<?php | |
$output = ''; | |
if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest'){ | |
$properties =& $scriptProperties; | |
$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() . '/' . 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($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']; | |
} | |
} else { | |
$output = $modx->getChunk($defaultChunk); | |
} | |
return $output; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment