Dear Sottwell
Thanks very much for your reply. It’s much appreciated. I can’t say that I fully understood your response though! Fortunately, after a few hours mulling it over I managed to find quite a good and simple solution, and it involves a plugin like your solution suggested too. Here it is for anyone that’s interested:
a) Use php code directly within the template to choose between the two template chunks. At this stage MODx will treat this PHP as if it is normal text and wont evaluate it. Therefore, the actual document created will contain both handheld and web content and it can all be cacheable in the normal MODx way since none of it is within a non-cacheable snippet. The template might look something like this...
<?php
if ( ! function_exists( 'is_handheld' ) )
{
function is_handheld()
{
// analysis of headers...
}
}
if ( is_handheld() )
{
?>
<html ...>
<!-- handheld template goes here and is subject to normal MODx caching -->
</html>
<?php
}
else
{
?>
<html ...>
<!-- web template goes here and is subject to normal MODx caching -->
</html>
<?php
}
?>
b) The next step is to make sure that this residual php code gets evaluated after the document has been constructed or retrieved from the cache but before it gets served. When the php gets evaluated it will filter out the correct part of the (cached) document. To do this, we create a plugin called phpEvalDocument, say, set it to be the last plugin that is executed on the OnWebPagePrerender event, and put this code in it:
ob_start();
eval('?>' . $modx->documentOutput . '<?php ');
$modx->documentOutput = ob_get_contents();
ob_end_clean();
This code evaluates the remaining php in the document, captures the evaluated php and reassigns the documentOutput.
The good thing about this approach is that it is even possible to export and use static html versions of the website. Just disable the phpEvalDocument plugin temporarily while doing the export and the php will remain in the document. Then all you would need to do is to use the .htaccess file to tell the server to evaluate the php in the static file version of the document, since it wouldn’t normaly do this if the file had an html extension for example. Then you can have a partly-dynamic static website!
Cheers,
Paul