PHPackages                             unprefix/template-loader - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. unprefix/template-loader

ActiveLibrary[Templating &amp; Views](/categories/templating)

unprefix/template-loader
========================

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

4.1.0(7y ago)2179[4 issues](https://github.com/widoz/template-loader/issues)GPL-2.0-onlyPHPCI failing

Since May 15Pushed 7y ago1 watchersCompare

[ Source](https://github.com/widoz/template-loader)[ Packagist](https://packagist.org/packages/unprefix/template-loader)[ RSS](/packages/unprefix-template-loader/feed)WikiDiscussions develop Synced 3d ago

READMEChangelog (7)Dependencies (4)Versions (9)Used By (0)

[![Build Status](https://camo.githubusercontent.com/c546bc8304e20ed3f994de1273179e565156129f6c3bf3d21c61a2d5e1f7fa85/68747470733a2f2f7472617669732d63692e6f72672f7769646f7a2f74656d706c6174652d6c6f616465722e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/widoz/template-loader)[![codecov](https://camo.githubusercontent.com/cb08bc91a28e9eafe7dc026102187bd8cb5a4ec769528eaa9dad1be99289f4ec/68747470733a2f2f636f6465636f762e696f2f67682f7769646f7a2f74656d706c6174652d6c6f616465722f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/widoz/template-loader)

WordPress Template Loader
=========================

[](#wordpress-template-loader)

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

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

Requirements
------------

[](#requirements)

Php &gt;= 5.6.x

Examples
--------

[](#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.

```
$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.

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.

```
$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`.

Data Type
---------

[](#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.

This way we can extends the interface to create our own contracts based on the specific view.

Hooks
-----

[](#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.

There are two filter: `tmploader_template_engine_data` that is generic and pass the `$data` value and the `slug` property.

```
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.

```
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
------------

[](#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.

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.

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.

```
$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`.

Avoiding unnecessary filesystem access that we know are slows.

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance0

Infrequent updates — may be unmaintained

Popularity13

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity68

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~94 days

Recently: every ~113 days

Total

7

Last Release

2722d ago

Major Versions

1.0.0 → 2.0.02017-05-21

2.1.0 → 3.0.02017-09-29

3.0.0 → 4.0.02018-02-17

### Community

Maintainers

![](https://www.gravatar.com/avatar/6d39b8d5cede59a2123551e756824be76d75405fab12f4e553be05328e9b1341?d=identicon)[guido](/maintainers/guido)

---

Top Contributors

[![widoz](https://avatars.githubusercontent.com/u/1917784?v=4)](https://github.com/widoz "widoz (112 commits)")

---

Tags

loaderoopphppsrtemplatetemplate-loaderwordpress

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/unprefix-template-loader/health.svg)

```
[![Health](https://phpackages.com/badges/unprefix-template-loader/health.svg)](https://phpackages.com/packages/unprefix-template-loader)
```

###  Alternatives

[mustache/mustache

A Mustache implementation in PHP.

3.3k44.6M291](/packages/mustache-mustache)[roots/acorn

Framework for Roots WordPress projects built with Laravel components.

9682.1M97](/packages/roots-acorn)[whitecube/nova-flexible-content

Flexible Content &amp; Repeater Fields for Laravel Nova.

8053.0M25](/packages/whitecube-nova-flexible-content)[mopa/bootstrap-bundle

Easy integration of twitters bootstrap into symfony2

7042.9M33](/packages/mopa-bootstrap-bundle)[limenius/react-bundle

Client and Server-side react rendering in a Symfony Bundle

3871.2M](/packages/limenius-react-bundle)[nicmart/string-template

StringTemplate is a very simple string template engine for php. I've written it to have a thing like sprintf, but with named and nested substutions.

2101.7M30](/packages/nicmart-string-template)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
