OnWebPagePrerender

< ‌ index ‌ >

This is the final event before the completed HTML document is sent to the server. It makes it possible to work with the document as a string, using the $modx->documentOutput variable.

Here is the code at the end of the outputContent function in the document parser:

// invoke OnWebPagePrerender event
if(!$noEvent) {
$this->invokeEvent("OnWebPagePrerender");
}
echo $this->documentOutput;
ob_end_flush();
}

One example of using this event is to add a value to the URL of links in the finished page. Here is a plugin that does just that, for a multi-language site.

The plugin is set to listen to the OnWebPagePrerender event.

event listener

The plug-in gets the documentOutput string and does a search-and-replace to insert the lang value in the URL query strings of the links in the page.

$lang = isset($_GET['lang'])? $_GET['lang'] : $modx->config['site_start'];
$output = $modx->documentOutput;
$modx->documentOutput = preg_replace("#(]+href=[^"]'?)([^>' ]+)('?[^>]*>)#is", "\1\2&lang=$lang\3", $output);

The modified string is returned to the parser, where it is sent off to the server.

So any time you want to work with the finished HTML page before it gets sent to the user, the OnWebPagePrerender event is the one you want to listen for.

< ‌ index ‌ >