Template
Easily include and pass data to theme templates.
Requirements
By default, the file must be located within the the theme's templates
folder. Subfolders are fine as the directory is traversed recursively.
When working with child themes it works as you'd expect, child theme templates have precedence over parent theme templates.
Simple Include
use WPDev\Template\Template;
Template::include('reusable-component.php');
// or with global function
template('reusable-component.php');
Include with Data
use WPDev\Template\Template;
Template::include('reusable-component.php', ['title' => 'So Easy']);
// or with global function
template('reusable-component.php', ['title' => 'So Easy']);
API Reference
Public Methods
Method | Summary |
__construct |
Constructor. Alternatively use Template::include() or Template::locate() .
|
create
static |
For a more fluid syntax. Alternatively use Template::include() or Template::locate() .
|
getTemplate | |
include
static |
Include a theme template file. Optionally pass data. |
includeTemplate | Includes the template file passing along the data. |
locate
static |
Locates a template file. |
-
__construct
__construct( string
$file_name
[, array$data
] )SummaryConstructor. Alternatively useTemplate::include()
orTemplate::locate()
.Parametersfile_namedata -
create
create( string
$file_name
[, array$data
] )SummaryFor a more fluid syntax. Alternatively useTemplate::include()
orTemplate::locate()
.Parametersfile_namedataReturns$this
-
getTemplate
getTemplate( )
-
include
include( string
$file_name
[, array$data
] )SummaryInclude a theme template file. Optionally pass data.Parametersfile_nameThe file name of the template.dataData to be passed to view. Will also be extracted into variables.Returnsbool
True if successfully included the template. Otherwise, false.
-
includeTemplate
includeTemplate( )
SummaryIncludes the template file passing along the data.Returnsbool
True if successfully included the template. Otherwise, false.
-
locate
locate( string
$file_name
)SummaryLocates a template file.Parametersfile_nameReturnsstring
The path to the template file. Empty if none found.