$page→renderPage()
Render page output
This method is essentially the same as the render() method except that the render() method
is a gateway to this method or the renderField() method, and this one is not. More specifically:
- This method has only one purpose, which is rendering page output.
- This method has only has one argument, which is always an array.
- This method is available only in ProcessWire 3.0.253+.
This method is preferable to render() when it comes hooks or overriding in custom page classes,
as you don't need to figure out anything about the arguments.
Available since version 3.0.253.
Example
// regular page render call
echo $page->renderPage();
// render while passing in custom variables
echo $page->renderPage([
'firstName' => 'Ryan',
'lastName' => 'Cramer'
]);
// in your template file, you can access the passed-in variables like this:
echo "<p>Full name: $options[firstName] $options[lastName]</p>";
// hoooking this method
$wire->addHookAfter('Page::renderPage', function(HookEvent $event) {
$event->return = str_replace("</body>", "<p>Hello</p></body>", $event->return);
});Usage
$string = $page->renderPage(array $options);Arguments
| Name | Type(s) | Description |
|---|---|---|
$options | array | Custom variables to pass to template file, and/or options as described below:
|
Return value
string mixedRenders the rendered output
Exceptions
Method can throw exceptions on error:
WireException
Hooking $page→renderPage(…)
You can add your own hook events that are executed either before or after the $page method is executed. Examples of both are included below. A good place for hook code such as this is in your /site/ready.php file.
Hooking before
The 'before' hooks are called immediately before each $page method call is executed. This type of hook is especially useful for modifying arguments before they are sent to the method.
$this->addHookBefore('Page::renderPage', function(HookEvent $event) {
// Get the object the event occurred on, if needed
$page = $event->object;
// Get values of arguments sent to hook (and optionally modify them)
$options = $event->arguments(0);
/* Your code here, perhaps modifying arguments */
// Populate back arguments (if you have modified them)
$event->arguments(0, $options);
});Hooking after
The 'after' hooks are called immediately after each $page method call is executed. This type of hook is especially useful for modifying the value that was returned by the method call.
$this->addHookAfter('Page::renderPage', function(HookEvent $event) {
// Get the object the event occurred on, if needed
$page = $event->object;
// An 'after' hook can retrieve and/or modify the return value
$return = $event->return;
// Get values of arguments sent to hook (if needed)
$options = $event->arguments(0);
/* Your code here, perhaps modifying the return value */
// Populate back return value, if you have modified it
$event->return = $return;
});API reference based on ProcessWire core version 3.0.252