Skip to content

Template class

Javier edited this page Oct 22, 2021 · 3 revisions

Important: This documentation is still under construction, methods and/or properties may be missing or not fully documented


The Template class is a helper that allows to generate an alternative to get_template_part in your plugin, this provides a simple mechanism for themes (or child themes) to overload reusable sections of code in the plugin.

Includes the named template part for a theme or if a name is specified then a specialized part will be included. If the theme contains no {slug}.php file then no template will be included.

The template is included using require, not require_once, so you may include the same template part multiple times.

For the $name parameter, if the file is called "{slug}-special.php" then specify "special".


Generating a custom function for your plugin

Is recommended define a constant to root folder of your plugin in the plugin root file

define('MY_PLUGIN_TEMPLATE_PATH', __DIR__ . '/templates')

Then you can generate a custom function get_template_part for your plugin

use JPToolkit\TemplateHelper\Template;

/**
 * @param string      $slug The slug name for the generic template.
 * @param string|null $name The name of the specialized template.
 * @param array       $args Additional arguments passed to the template.
 */
function my_plugin_get_template_part($slug, $name = null, $args = [])
{
  static $template;

  if (empty($template)) {
    // Absolute path of your template folder in your plugin
    $plugin_template_path = MY_PLUGIN_TEMPLATE_PATH;
    // Folder that must be contains your templates in your theme
    // to overload plugin templates
    $stylesheet_path = 'my-plugin';
    // optional, this is used to prefix actions and filters
    $namespace       = 'my_plugin';

    $template = new Template($plugin_template_path, $theme_folder, $prefix);
  }

  $template->get_template_part($slug, $name, $args);
}

This function use the same parameters and preserve all the functionality of get_template_part with the difference that it searches in the defined directory instead of the root of the theme. For more details about theget_template_part function and parameters please read this

Usage of your custom function

<?php
my_plugin_get_template_part('loop/item','mobile')

In this example the function search the requested template in this order:

  1. {child-theme-folder}/my-plugin/loop/item-mobile.php
  2. {parent-theme-folder}/my-plugin/loop/item-mobile.php
  3. {plugin-folder}/templates/loop/item-mobile.php
  4. {child-theme-folder}/my-plugin/loop/item.php
  5. {parent-theme-folder}/my-plugin/loop/item.php
  6. {plugin-folder}/templates/loop/item.php.

Actions

{prefix}_get_template_part

This action is triggered before a template part is loaded.

Params

$slug The slug name for the generic template.

​ * @param string $name The name of the specialized template.

​ * @param string[] $templates Array of template files to search for, in order.

​ * @param array $args Additional arguments passed to the template.

See Also


Change log

Version Description
1.2.0 Introduced

Clone this wiki locally