Skip to content

Instantly share code, notes, and snippets.

@andronex
Forked from joeke/parse-resource.php
Created October 14, 2024 17:10
Show Gist options
  • Save andronex/237440d9eec85c50a251b51c54f10c3d to your computer and use it in GitHub Desktop.
Save andronex/237440d9eec85c50a251b51c54f10c3d to your computer and use it in GitHub Desktop.
MODX Revo: Parse full contents of resource
<?php
/**
* 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment