$page→render()
Render output of this Page or a Field
You may optionally specify 1-2 arguments to the method. The first argument may be any of the following:
- An
$optionsarray with predefined options or custom variables (see arguments definition). - A filename to use for render (in /site/templates/). When used, you can optionally specify an
$optionsarray as the 2nd argument. - A field name to render. An optional 2nd argument can be a filename (in /site/templates/fields/) to use to render the field, though we'd recommend just using the
renderField()method instead.
If using the $options array argument (whether 1st or 2nd argument), you may use it to specify your own variables to pass along to the
template file, and those values will be available in a variable named $options within the scope of the template file
(see examples below).
Note: If the page’s template has caching enabled, then this method will return a cached page render (when available) or save a new cache. Caches are only saved on guest users.
For simpler and more specific methods, we recommend using, hooking or overriding renderPage() or renderField() instead.
Example
// regular page render call
$out = $page->render();
// render using given file (in /site/templates/)
$out = $page->render('basic-page.php');
// render while passing in custom variables
$out = $page->render([
'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>";Usage
// basic usage
$string = $page->render();
// usage with all arguments
$string = $page->render($options = [], array $options2 = null);Arguments
| Name | Type(s) | Description |
|---|---|---|
$options (optional) | array string | String of filename to use for render, field name to render, or array of options:
|
$options2 (optional) | array | If a filename or field name was used for $options then you may optionally specify options array here instead. |
Return value
string mixed
Exceptions
Method can throw exceptions on error:
WireException
Hooking $page→render(…)
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::render', 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);
$options2 = $event->arguments(1);
/* Your code here, perhaps modifying arguments */
// Populate back arguments (if you have modified them)
$event->arguments(0, $options);
$event->arguments(1, $options2);
});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::render', 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);
$options2 = $event->arguments(1);
/* Your code here, perhaps modifying the return value */
// Populate back return value, if you have modified it
$event->return = $return;
});See Also
API reference based on ProcessWire core version 3.0.253