$pagerenderPage()

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

NameType(s)Description
$optionsarray

Custom variables to pass to template file, and/or options as described below:

  • foo_bar (mixed): Specify any of your own variable names and values to send to the template file (foo_bar is just an example, use your own).
  • filename (string): Filename to render, typically relative to /site/templates/. Absolute paths must resolve somewhere in PW’s install. Default:''
  • prependFile (string): Filename to prepend to output, must be in /site/templates/.
  • prependFiles (array): Array of additional filenames to prepend to output, must be relative to /site/templates/.
  • appendFile (string): Filename to append to output, must be in /site/templates/.
  • appendFiles (array): Array of additional filenames to append to output, must be relative to /site/templates/.
  • allowCache (bool): Allow cache to be used when template settings ask for it? Default:true
  • forceBuildCache (bool): If true, the cache will be re-created for this page. Default:false
  • Note that the prepend and append options above have default values in $config or with the Template.

Return value

string mixed

Renders 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->renderPage(…) 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->renderPage(…) 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->renderPage(…) 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;
});

$page methods and properties

API reference based on ProcessWire core version 3.0.252