We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 8784
    • 32 Posts
    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.)
    • I think you should provide some contextual information on how you are executing / using this script.

      In the meantime, you don't need to manually get a user object; this is done in the initialize() method from the session. And your loop is problematic in multiple ways.

      First, using getCollection() like that loads ALL of the Resources into memory at once. This is very bad; you should try using getIterator() instead, which loads one Resource into memory at a time.

      Second, you do not need to manually get the Template object like that. Resources already know about their templates and using the process() method already available on the Resource object will handle the Template itself.

      Finally, this will not process the non-cacheable content for each Resource. For that, you will need to complete the parsing process similar to how it is done in modResponse->outputContent().
        • 8784
        • 32 Posts
        Quote from: opengeek at Jan 31, 2012, 03:19 PM

        First, using getCollection() like that loads ALL of the Resources into memory at once. This is very bad; you should try using getIterator() instead, which loads one Resource into memory at a time.


        I should quickly clarify the actual getCollection call would only grab a handful of resources (not all of them). I didn't include that as it's not the bit where I'm having trouble.

          • 3749
          • 24,544 Posts
          Does your template have:

          [[*pagetitle]]


          instead of

          [[+pagetitle]]


          ??

          ---------------------------------------------------------------------------------------------------------------
          PLEASE, PLEASE specify the version of MODX you are using . . . PLEASE!
          MODx info for everyone: http://bobsguides.com/modx.html [ed. note: BobRay last edited this post 12 years, 3 months ago.]
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 8784
            • 32 Posts
            Hey BobRay,
            Yes using:
            [[*pagetitle]]
              • 3749
              • 24,544 Posts
              In a Tpl chunk, that will always show you the ID of the current page, rather than the one retrieved by the snippet.

              Try:

              [[+pagetitle]]



              ---------------------------------------------------------------------------------------------------------------
              PLEASE, PLEASE specify the version of MODX you are using . . . PLEASE!
              MODx info for everyone: http://bobsguides.com/modx.html
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 8784
                • 32 Posts
                I'm trying to grab/parse page templates not chunks. If I use a plus (+pagetitle) in the template, it does not parse at all. Just outputs the literal like this:
                <title>My Site Name - [[+pagetitle]]</title>