Hi modx folks,
I'm using the modx api mode to:
1. Get collection of resources
2. Parse the full html output of each
Unfortunately, when looping through the results, processing each temple/content, the "*pagetitle" is always the that of the first resource in the array. Strangely though the "*content" is okay.
Here is working code:
<?php
// Define the modx context, api:
define('MODX_API_MODE', true);
define('MODX_BASE_PATH', '/Applications/MAMP/htdocs/projects-osu/modxRevo2.2-rc/');
$base_url = '/fu';
if (!defined('MODX_BASE_URL')) define('MODX_BASE_URL', $base_url);
require_once MODX_BASE_PATH.'config.core.php';
require_once MODX_CORE_PATH.'config/'.MODX_CONFIG_KEY.'.inc.php';
require_once MODX_CORE_PATH.'model/modx/modx.class.php';
$modx = new modX();
$modx->initialize('web');
// Set a user: Wayfinder snippet requires user context to operate!
$modx->user = $modx->getObjectGraph('modUser',
array('Profile' => array()),
array('username' => 'Me', false)); // Replace 'Me' with an actual username
// Get not deleted, published and searchable documents:
$documents = $modx->getCollection('modResource',array(
'deleted' => 0,
'published' => 1,
'context_key' => 'web'
));
// Loop through each document's content:
if(!empty($documents)){
foreach($documents as $document){
$modx->resource =& $document; // Not sure if '=' or '=&' should be used
// Confirm CORRECT page title is present:
echo $modx->resource->get('pagetitle').' is Correct Page title';
// Create a temp object, place documents content into it:
$templateID = $modx->resource->get('template'); //Get template id used by doc
$templateHTML = $modx->getObject('modTemplate',$templateID);
$templateOutput = $templateHTML->get('content');
$tmp = $modx->newObject('modTemplate'); // 'modChunk' and 'modResource' don't work either
$tmp->setCacheable(false);
$tmp->set('pagetitle', $modx->resource->get('pagetitle')); // This has no affect either.
$tmp->setContent($templateOutput);
// The INCORRECT page title is now present in this output:
$output = $tmp->process();
echo $output;
// An attempt to unset vars to clear the sticking 'pagetitle'. None work:
unset($tmp);
unset($templateID);
unset($templateHTML);
unset($templateOutput);
unset($modx->resource);
unset($document);
}
}
The HTML output all have the same 'pagetitle'. For example, if your first document in the array is titled "Homepage", all others will have this too. But their "*content" is unique to each document as expected.
Any ideas?
(Mistakenly posted this in Revo 2.2 forum, moved to Q/A. Sorry.)