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 ] )

    Summary
    Constructor. Alternatively use Template::include() or Template::locate().
    Parameters
    file_name
    data
  • create

    create( string $file_name [, array $data ] )

    Summary
    For a more fluid syntax. Alternatively use Template::include() or Template::locate().
    Parameters
    file_name
    data
    Returns

    $this

  • getTemplate

    getTemplate( )

  • include

    include( string $file_name [, array $data ] )

    Summary
    Include a theme template file. Optionally pass data.
    Parameters
    file_name
    The file name of the template.
    data
    Data to be passed to view. Will also be extracted into variables.
    Returns

    bool

    True if successfully included the template. Otherwise, false.

  • includeTemplate

    includeTemplate( )

    Summary
    Includes the template file passing along the data.
    Returns

    bool

    True if successfully included the template. Otherwise, false.

  • locate

    locate( string $file_name )

    Summary
    Locates a template file.
    Parameters
    file_name
    Returns

    string

    The path to the template file. Empty if none found.