We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17499 ☆ A M B ☆
    • 872 Posts
    Hello,
    I want to parse a Chunk before sending back the data to the browser?

    Here is a simple Chunk

    <div class="[[+myclass]]">
    [[+myContent]]
    
    [[!anotherSnippet?properties=`xxx`]]
    </div>
    
    


    Now when i use getChunk, all placeholders are treated but the snippet call remain untouched and is sended back to the browser unparsed as plain text.

    <?php
    /* All database treatment has been done */
    $data = $modx->mycomponent->getChunk('chunk', $arr);
    
    // This is where i want to parse my Chunk for tags that are not placeholders
    // And then send the processed chunk to the browser
    
    $response['success'] = true;
    $response['data'] = $data;
    return $this->modx->toJSON($response);
    


    Is it possible?
    Is there a better solution?
      • 22303 MODX Staff
      • 10,725 Posts
      If I understand correctly that the results of this script are not being processed by MODx, all you’ll need to do is add this after your getChunk call:
      <?php
      
      /* get the max iterations tags are processed before processing is terminated */
      $maxIterations= (integer) $modx->getOption('parser_max_iterations', null, 10);
      
      /* parse all cacheable tags first */
      $modx->getParser()->processElementTags('', $data, false, false, '[[', ']]', array(), $maxIterations);
      
      /* parse all non-cacheable and remove unprocessed tags */
      $modx->getParser()->processElementTags('', $data, true, true, '[[', ']]', array(), $maxIterations);
      
      ?>


      However, if the script is a Snippet being executed on a Resource, it should get processed automatically before being returned as a MODx Resource.
        • 17499 ☆ A M B ☆
        • 872 Posts
        Thanks OpenGeek, that worked like a charm.

          • 17499 ☆ A M B ☆
          • 872 Posts
          If someone is interested, there is no need to load the parser and iterate if you’re using the input and Output options of MODx Revolution(phx like).

          That’s great! smiley