We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 10378
    • 375 Posts
    Hi,

    I'm still trying to get the parsed content of a resource and save the output in a variable. Using the following code - the resource fields (*id, *pagetitle, *content, ...) aren't parsed. The MODX tags are simply removed from output. Placeholders and setting tags are parsed:

    public function getHtml($id) {
    
        $this->resource = $this->modx->getObject('modResource', $id);
        $html = $this->resource->process();
             
        $maxIterations = intval($this->modx->getOption('parser_max_iterations', null, 10));
             
        // Process the non-cacheable content of the Resource, but leave any unprocessed tags alone
        $this->modx->parser->processElementTags('', $html, true, false, '[[', ']]', array(), $maxIterations);
             
        // Process the non-cacheable content of the Resource, this time removing the unprocessed tags
        $this->modx->parser->processElementTags('', $html, true, true, '[[', ']]', array(), $maxIterations);
            
        return $html;
    }
    


    Also the MODX error log shows an error:

    `[[*id]]` is not a valid integer and may not be passed to makeUrl()



    Here is the (simplified) template which is assigned to the resource (no chunks are used):

    <!DOCTYPE html>
    <html lang="[[++cultureKey]]">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=[[++modx_charset]]" />    
        <title>[[++site_name]] | [[*pagetitle]]</title>
        <base href="[[++site_url]]" />
    </head>
    <body>
    <header>
        [[++site_name]]
    </header>
    <main>
        <h1><a href="[[~[[*id]]]]">[[*pagetitle]]</a></h1>
        [[*introtext:notempty=`
            <div>[[*introtext]]</div>
        `]]
        <div>
            [[*content]]
        </div>
        <div>
            Content of placeholder1 (cached): [[+placeholder1]]<br>
            Content of placeholder2: [[!+placeholder2]]
        </div>
    <main>
    </body>
    </html>
    


    I had a look at @opengeeks renderResources snippet, but can't see whats wrong:
    https://github.com/opengeek/renderResources/blob/develop/core/components/renderresources/snippet.renderresources.php

    This question has been answered by multiple community members. See the first response.

      Freelancer @bitego http://www.bitego.com
      ---
      GoodNews - one of the most advanced and integrated Group Mailer premium add-ons for MODX Revolution!
      More infos here: http://www.bitego.com/extras/goodnews/
    • discuss.answer
      • 22303 MODX Staff
      • 10,725 Posts
      There are a few global environment adjustments you need to make before parsing the Resource content...

      Starting at line 399 you see some values being stored for later restoration:
      $currentResource = $modx->resource;
      $currentResourceIdentifier = $modx->resourceIdentifier;
      $currentElementCache = $modx->elementCache;


      Then at line 446 the changes are made to prepare to process the Resource...
      $modx->resource = $resource;
      $modx->resourceIdentifier = $resource->get('id');
      $modx->elementCache = array();


      Then finally at line 461 I restore the original values...
          $modx->elementCache = $currentElementCache;
      $modx->resourceIdentifier = $currentResourceIdentifier;
      $modx->resource = $currentResource;


      The Resource having access to itself via $modx->resource is critical for getting many of those values you are missing, as well as for proper execution of certain Snippets that may appear in the content.
      • discuss.answer
        • 10378
        • 375 Posts
        Hi @opengeek,

        your hints where perfect! Finally I have a fully working method for getting the full parsed content of a resource!

        Here is the final method so others can also profit from your help too:

        /**
         * Get full parsed HTML from resource
         * (you already need to have the resource object available in $this->resource)
         * 
         * @access public
         * @return string $html The parsed html of the resource
         */
        public function parseResource() {
        
            // Store for later restoration
            $currentResource = $this->modx->resource;
            $currentResourceIdentifier = $this->modx->resourceIdentifier;
            $currentElementCache = $this->modx->elementCache;
            
            // Changes are made to prepare for processing the Resource
            $this->modx->resource = $this->resource;
            $this->modx->resourceIdentifier = $this->resource->get('id');
            $this->modx->elementCache = array();
                    
            // The Resource having access to itself via $this->modx->resource is critical 
            // for getting resource fields, as well as for proper execution of Snippets 
            // that may appear in the content.
        
            // Process and return the cacheable content of the Resource
            $html = $this->modx->resource->process();
        
            // Restore the original values
            $this->modx->elementCache = $currentElementCache;
            $this->modx->resourceIdentifier = $currentResourceIdentifier;
            $this->modx->resource = $currentResource;
             
            // Determine how many passes the parser should take at a maximum
            $maxIterations = intval($this->modx->getOption('parser_max_iterations', null, 10));
             
            if (!$this->modx->parser) {
                $this->modx->getParser();
            }
        
            // Process the non-cacheable content of the Resource, but leave any unprocessed tags alone
            $this->modx->parser->processElementTags('', $html, true, false, '[[', ']]', array(), $maxIterations);
             
            // Process the non-cacheable content of the Resource, this time removing the unprocessed tags
            $this->modx->parser->processElementTags('', $html, true, true, '[[', ']]', array(), $maxIterations);
             
            return $html;
        }
        

          Freelancer @bitego http://www.bitego.com
          ---
          GoodNews - one of the most advanced and integrated Group Mailer premium add-ons for MODX Revolution!
          More infos here: http://www.bitego.com/extras/goodnews/