PHPackages                             underpin/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. underpin/template-loader

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

underpin/template-loader
========================

Template loader for Underpin. Intended to be used by WordPress themes.

1.2.0(4y ago)02711GPL-2.0-or-laterPHP

Since Aug 14Pushed 4y ago1 watchersCompare

[ Source](https://github.com/Underpin-WP/template-loader)[ Packagist](https://packagist.org/packages/underpin/template-loader)[ RSS](/packages/underpin-template-loader/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (2)Dependencies (1)Versions (5)Used By (1)

Underpin Template Loader
========================

[](#underpin-template-loader)

Loader That assists with registering templates to themes.

**NOTE: If you're building a WordPress plugin, you probably don't need this, you probably just need the [Template Trait](github.com/underpin-WP/underpin#template-system-trait) built directly into Underpin.**

This loader makes it possible to create custom templates to be used arbitrarily in your theme. It replaces `get_template_part`, and adds some useful beneftis:

1. It provides a clear place to put logic, data-fetching, and other things of the sort.
2. Templates can be nested.
3. Templates can selectively be overridden, and extended by the child theme

Installation
------------

[](#installation)

### Using Composer

[](#using-composer)

`composer require underpin/template-loader`

### Manually

[](#manually)

This plugin uses a built-in autoloader, so as long as it is required *before* Underpin, it should work as-expected.

`require_once(__DIR__ . '/underpin-templates/templates.php');`

Setup
-----

[](#setup)

1. Install Underpin. See [Underpin Docs](https://www.github.com/underpin-wp/underpin)
2. Register new templates as-needed.

### Registering a Template

[](#registering-a-template)

Like any loader item, Templates must be registered. This can be done in a few different ways.

```
underpin()->templates()->add( 'index', [
	'description' => "Renders the home page.", // Human-readable description
	'name'        => "Index Template.",        // Human-readable name
	'group'       => 'index',                  // Template group.
	'root_path'   => underpin()->template_dir()// Template path
	'templates'   => [                         // Templates to include.
		'loop'     => 'public',
		'post'     => 'public',
		'no-posts' => 'public',
	],
] );
```

The above example would expect three templates inside the `templates/index` directory.

1. `loop`
2. `post`
3. `no-posts`

This is a great down-and dirty way to set up a template, however some more-complex templates will benefit from using custom class methods inside the template. In these cases, it makes more sense to extend `Theme\Abstracts\Template` so you can add your own logic and keep your markup clean.

You can register a template as a class directly, like so:

```
// lib/templates/Index.php

class Index extends \Theme\Abstracts\Template{

  protected $name = 'Index Template';

  protected $description = 'Human Read-able description';

  protected $group = 'index';

  protected $templates = [                         // Templates to include.
		'loop'     => 'public',
		'post'     => 'public',
		'no-posts' => 'public',
	];

  // Optionally place any helper methods specific to this template here. These methods would be use-able inside of the
  // template, and can really help keep your templates clean.
}
```

And then register your template in `functions.php` like so:

```
underpin()->templates()->add('index','Theme\Templates\Index');
```

### Rendering Template Output

[](#rendering-template-output)

The purpose of a template is to render the output HTML. Ideally, all of your logic would be pre-determined, and passed directly to your template so it can be accessed directly via `get_param()`.

Let's take a look at the basic WordPress loop using the template system:

```
