$files->render() method

Given a filename, render it as a ProcessWire template file

This is a shortcut to using the TemplateFile class.

File is assumed relative to /site/templates/ (or a directory within there) unless you specify a full path. If you specify a full path, it will accept files in or below any of the following:

  • /site/templates/
  • /site/modules/
  • /wire/modules/

Note this function returns the output to you, so that you can send the output wherever you want (delayed output). For direct output, use the $files->include() function instead.

Usage

// basic usage
$string = $files->render(string $filename);

// usage with all arguments
$string = $files->render(string $filename, array $vars = [], array $options = []);

Arguments

NameType(s)Description
filenamestring

Assumed relative to /site/templates/ unless you provide a full path name with the filename. If you provide a path, it must resolve somewhere in site/templates/, site/modules/ or wire/modules/.

vars (optional)array

Optional associative array of variables to send to template file. Please note that all template files automatically receive all API variables already (you don't have to provide them).

options (optional)array

Associative array of options to modify behavior:

  • defaultPath (string): Path where files are assumed to be when only filename or relative filename is specified (default=/site/templates/)
  • autoExtension (string): Extension to assume when no ext in filename, make blank for no auto assumption (default=php)
  • allowedPaths (array): Array of paths that are allowed (default is templates, core modules and site modules)
  • allowDotDot (bool): Allow use of ".." in paths? (default=false)
  • throwExceptions (bool): Throw exceptions when fatal error occurs? (default=true)
  • cache (int|string|Page): Specify non-zero value to cache rendered result for this many seconds, or see the WireCache::renderFile() method $expire argument for more options you can specify here. (default=0, no cache) Note: this option added in 3.0.130

Return value

string bool

Rendered template file or boolean false on fatal error (and throwExceptions disabled)

Exceptions

Method can throw exceptions on error:

  • WireException - if template file doesn't exist

See Also


$files methods and properties

API reference based on ProcessWire core version 3.0.244

Latest news

  • ProcessWire Weekly #559
    The 559th issue of ProcessWire Weekly brings in all the latest news from the ProcessWire community. Modules, sites, and more. Read on!
    Weekly.pw / 25 January 2025
  • ProcessWire 3.0.244 new main/master version
    ProcessWire 3.0.244 is our newest main/master/stable version. It's been more than a year in the making and is packed with tons of new features, issue fixes, optimizations and more. This post covers all the details.
    Blog / 18 January 2025
  • Subscribe to weekly ProcessWire news

“ProcessWire is like a breath of fresh air. So powerful yet simple to build with and customise, and web editors love it too.” —Margaret Chatwin, Web developer