Plugin Template
Easily include and pass data to theme overridable plugin templates.
Requirements
This works generally the same way as the regular WPDev\Template\Template
helper, but with one key difference.
Instead of passing in just the filename of the template, an absolute path to the plugin's template file should be passed in. This is the file that will be used if there isn't an overriding template found in the child/parent themes.
Override in Theme
To override a plugin's template file in your theme, the file must be located in templates/plugins/{{plugin-folder-name}}/{{name-of-template.php}}
For example if the plugin you're overriding is in a folder called my-plugin, then it will look for templates in the child/parent theme's templates/plugins/my-plugin/
folder. This is to help avoid template naming conflicts between plugins. It also has the benefit of keeping the templates folder organized.
Simple Include
use WPDev\Template\PluginTemplate;
PluginTemplate::include(__DIR__.'/reusable-component.php');
// or with global function
plugin_template(__DIR__.'/reusable-component.php');
Include with Data
use WPDev\Template\PluginTemplate;
PluginTemplate::include(__DIR__.'/reusable-component.php', ['title' => 'So Easy']);
// or with global function
plugin_template(__DIR__.'/reusable-component.php', ['title' => 'So Easy']);
API Reference
Public Methods
Method | Summary |
__construct |
Locates and renders a theme template. Falls back to the absolute $file_path passed in.
|
includeTemplate | |
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. |
locate
static |
Locates a template file. |
-
__construct
__construct( string
$file_path
[, array$data
] )SummaryLocates and renders a theme template. Falls back to the absolute$file_path
passed in.Parametersfile_pathThe absolute path to the file in the plugin. The basename will be used when searching in the themes.dataData to be passed to the template. -
includeTemplate
includeTemplate( )
-
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.
-
locate
locate( string
$file_name
)SummaryLocates a template file.Parametersfile_nameReturnsstring
The path to the template file. Empty if none found.