2017 © Pedro Peláez
 

library template-loader

A simple hookable template loader for WordPress. Allow you to load templates in chain from child to plugin.

image

unprefix/template-loader

A simple hookable template loader for WordPress. Allow you to load templates in chain from child to plugin.

  • Sunday, June 3, 2018
  • by guido
  • Repository
  • 1 Watchers
  • 2 Stars
  • 168 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 0 Forks
  • 3 Open issues
  • 8 Versions
  • 0 % Grown

The README.md

Build Status codecov, (*1)

WordPress Template Loader

A simple hookable template loader for WordPress. Allow you to load templates in chain from child to plugin., (*2)

This is not for the WordPress templates in the form of "name-slug.php" with fallback to name.php. Indeed this is a loader to build a basic data injection for views (templates)., (*3)

Requirements

Php >= 5.6.x, (*4)

Examples

The TemplateLoader\Loader class make use of the Fluent interface, so it's possible to concatenate the calls to ask the instance to do the things., (*5)

$loader = new TemplateLoader\Loader('template_slug', new TemplateLoader\DataStorage());

$loader->withData(new ModelInterface())
       ->usingTemplate('/relative/file/path.php')
       ->render()

The class make use of WordPress function locate_template to locate the template file within the child and parent theme., (*6)

If you use the library within a plugin it's possible to define a fallback template part file path. As the name fallback says the file will be loaded only in case nothing is found into the previous locations., (*7)

$loader = new TemplateLoader\Loader('template_slug', new TemplateLoader\DataStorage());

$loader->withData(new ModelInterface())
       ->usingTemplate('/relative/file/path.php')
       ->butFallbackToTemplate('/plugin/relative/file/path.php')
       ->render();

Since WordPress function locate_template allow to pass an array along a string, we can do the same by passing an array of template paths to the method usingTemplate., (*8)

Data Type

The data type used to inject values into the template is a class named TemplateLoader\ModelInterface. ModelInterface doesn't declare any method. It's just a way to ensure the correct type of data is passed into the template loader., (*9)

This way we can extends the interface to create our own contracts based on the specific view., (*10)

Hooks

The render method, perform some filters that allow third party code to hook into the data and template to be modified before the template file is loaded., (*11)

There are two filter: tmploader_template_engine_data that is generic and pass the $data value and the slug property., (*12)

add_filter('tmploader_template_engine_data', function(TemplateLoader\ModelInterface $data, string $slug) {

    switch($slug) {
        case 'my_slug':
            // Do something
        break;

        default:
        break;
    }

    return $data;

});

The second one is similar but the filter name include the slug template: "tmploader_template_engine_data_{$this->slug}". This in case you don't want or need to write conditional statements to know which is the current processing template., (*13)

add_filter('tmploader_template_engine_data_my_template', function(TemplateLoader\ModelInterface $data) {
    return new DataTemplate(); // An new instance of a class implementing TemplateLoader\ModelInterface.
});

Performances

Since it's usually to call the same view in different portion of the same page, so whitin the same http request, to prevent to access multiple time to the file system only to know where the file template is located, we defined an internal collection that can be used to store the template file paths., (*14)

The second time we try to ask the same template, we'll not perform any additional filesystem access in order to load the template file. This improve speed for multiple calls. Also since we don't create a strict relation betwee the template and the data to inject, we can pass every time a different data value., (*15)

So, just to clarify with an example, during the same call we can instantiate the loader once and than ask to load the same template multiple times with different data values., (*16)

$loader = new TemplateLoader\Loader('template_slug', new TemplateLoader\DataStorage());

$loader->withData(new ModelInterface())
       ->usingTemplate('/relative/file/path.php')
       ->butFallbackToTemplate('/plugin/relative/file/path.php')
       ->render();

// Some code ... and then ...

$loader->withData(new ModelInterface())
       ->render();

The second time we call the render method we had changed only the data used within the template since we have stored the template path related with the slug of the template. In this case template_slug., (*17)

Avoiding unnecessary filesystem access that we know are slows., (*18)

The Versions

03/06 2018

dev-dev-master

dev-dev-master

A simple hookable template loader for WordPress. Allow you to load templates in chain from child to plugin.

  Sources   Download

GPL2.0 GPL-2.0-only

The Development Requires

05/03 2018

4.0.1

4.0.1.0

A simple hookable template loader for WordPress. Allow you to load templates in chain from child to plugin.

  Sources   Download

GPL-2.0-only

The Development Requires

17/02 2018

dev-master

9999999-dev

A simple hookable template loader for WordPress. Allow you to load templates in chain from child to plugin.

  Sources   Download

GPL2.0 GPL-2.0-only

The Development Requires

17/02 2018

4.0.0

4.0.0.0

A simple hookable template loader for WordPress. Allow you to load templates in chain from child to plugin.

  Sources   Download

GPL-2.0-only

The Development Requires

29/09 2017

3.0.0

3.0.0.0

A simple hookable template loader for WordPress. Allow you to load templates in chain from child to plugin.

  Sources   Download

GPL2.0

The Development Requires

03/09 2017

2.1.0

2.1.0.0

A simple hookable template loader for WordPress. Allow you to load templates in chain from child to plugin.

  Sources   Download

GPL2.0

The Development Requires

21/05 2017

2.0.0

2.0.0.0

A simple hookable template loader for WordPress. Allow you to load templates in chain from child to plugin.

  Sources   Download

GPL2.0

The Development Requires

15/05 2017

1.0.0

1.0.0.0

A simple hookable template loader for WordPress. Allow you to load templates in chain from child to plugin.

  Sources   Download

GPL2.0

The Development Requires