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;
}
`[[*id]]` is not a valid integer and may not be passed to makeUrl()
<!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>
This question has been answered by multiple community members. See the first response.
$currentResource = $modx->resource; $currentResourceIdentifier = $modx->resourceIdentifier; $currentElementCache = $modx->elementCache;
$modx->resource = $resource;
$modx->resourceIdentifier = $resource->get('id');
$modx->elementCache = array();$modx->elementCache = $currentElementCache; $modx->resourceIdentifier = $currentResourceIdentifier; $modx->resource = $currentResource;
/**
* 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;
}