PHPackages                             creativecoder/wp-template-controller - 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. creativecoder/wp-template-controller

ActiveLibrary

creativecoder/wp-template-controller
====================================

Abstract data generation for your WordPress templates

1.0.1(11y ago)1151MITPHPPHP &gt;=5.3.0

Since Jan 19Pushed 9y ago1 watchersCompare

[ Source](https://github.com/creativecoder/wp-template-controller)[ Packagist](https://packagist.org/packages/creativecoder/wp-template-controller)[ Docs](https://github.com/creativecoder/wp-template-controller)[ RSS](/packages/creativecoder-wp-template-controller/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependenciesVersions (8)Used By (0)

Template Controller for WordPress
=================================

[](#template-controller-for-wordpress)

This class allows you to abstract the data you need for your theme templates from the template files, themselves (like `page.php` and `single.php`). Use a controller file to generate the data, and let your template files focus on displaying that data.

Install
-------

[](#install)

Include the class and a controller file within your theme or plugin

```
// Example:
require_once( get_template_directory() . '/lib/classes/class-template-controller.php' );
require_once( get_template_directory() . '/controller.php' );
```

Usage
-----

[](#usage)

Within the controller file, extend the class to create a controller for your theme. Each method within the class should match a body class of a template where you want to display the data. The `common()` method fires for all templates. Don't forget to init the class.

```
class My_Controller extends Template_Controller {

	// Generates data for all templates
	public function common() {
		$this->add( 'hi', 'I load for every template on the site.' );
	}

	public function page() {
		$this->add( 'yo', 'I load for page.php and custom page templates.' );

		// Get recent posts to display on the page
		$this->add( 'recent_posts', get_posts( array(
			'post_type' => 'post',
			'posts_per_page' => 2,
				)
			)
		);
	}
}

My_Controller::init();
```

Within your template files, call the data you've generated.

```
// within page.php

// Store the data
$yo = get_tpl_data( 'yo' );

// Echo out the data
tpl_data( 'hi' );

// Use data as you normally would in template files
$recent_posts = tpl_data( 'recent_posts' );
foreach( $recent_posts as $post ) {
	echo $post->post_title;
}
```

Alternatively, you can call a global to get all of the data available for that template.

```
// within page.php

global $template_data;
extract( $template_data, EXTR_SKIP );

echo $yo;

foreach( $recent_posts as $post ) {
	echo $post->post_title;
}
```

Multiple Controllers
--------------------

[](#multiple-controllers)

You can create and init as many child classes as you would like. All will follow the same pattern, loading any methods that match any body class on a template. All store data statically in the parent class so that it can be easily called with the tpl\_data functions.

Thanks to @DesignPlug for the idea:

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community5

Small or concentrated contributor base

Maturity63

Established project with proven stability

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 ~105 days

Recently: every ~55 days

Total

7

Last Release

3505d ago

### Community

Maintainers

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

### Embed Badge

![Health badge](/badges/creativecoder-wp-template-controller/health.svg)

```
[![Health](https://phpackages.com/badges/creativecoder-wp-template-controller/health.svg)](https://phpackages.com/packages/creativecoder-wp-template-controller)
```

PHPackages © 2026

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