Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e9f04ef

Browse files
author
Grant Kinney
committedJan 19, 2015
Initial commit
0 parents  commit e9f04ef

File tree

5 files changed

+327
-0
lines changed

5 files changed

+327
-0
lines changed
 

‎.gitignore

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Always-ignore extensions #
2+
############################
3+
*.bak
4+
*.diff
5+
*.err
6+
*.orig
7+
*.log
8+
*.rej
9+
*.swo
10+
*.swp
11+
*.zip
12+
*.vi
13+
*~
14+
*.un~
15+
*.sass-cache
16+
17+
# OS generated files #
18+
######################
19+
*~
20+
.svn
21+
.cvs
22+
.DS_Store*
23+
._*
24+
.Spotlight-V100
25+
.Trashes
26+
Icon?
27+
ehthumbs.db
28+
Thumbs.db
29+
.cache
30+
.project
31+
.settings
32+
.tmproj
33+
*.esproj
34+
nbproject
35+
*.sublime*
36+
37+
# Folders to ignore #
38+
#####################
39+
.hg
40+
.svn
41+
.CVS
42+
.idea
43+
node_modules
44+
dist
45+
.rvmrc
46+
.vagrant
47+
vendor

‎LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Grant Kinney
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

‎README.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
Template Controller for WordPress
2+
=======================================
3+
4+
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.
5+
6+
Install
7+
-------
8+
9+
Include the class and a controller file within your theme or plugin
10+
11+
```php
12+
// Example:
13+
require_once( get_template_directory() . '/lib/classes/class-template-controller.php' );
14+
require_once( get_template_directory() . '/controller.php' );
15+
```
16+
17+
Usage
18+
-----
19+
20+
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.
21+
22+
```php
23+
class My_Controller extends Template_Controller {
24+
25+
// Generates data for all templates
26+
public function common() {
27+
$this->add( 'hi', 'I load for every template on the site.' );
28+
}
29+
30+
public function page() {
31+
$this->add( 'yo', 'I load for page.php and custom page templates.' );
32+
33+
// Get recent posts to display on the page
34+
$this->add( 'recent_posts', get_posts( array(
35+
'post_type' => 'post',
36+
'posts_per_page' => 2,
37+
)
38+
)
39+
);
40+
}
41+
}
42+
43+
My_Controller::init();
44+
```
45+
46+
Within your template files, call the data you've generated.
47+
48+
```php
49+
// within page.php
50+
51+
// Store the data
52+
$yo = get_tpl_data( 'yo' );
53+
54+
// Echo out the data
55+
tpl_data( 'hi' );
56+
57+
// Use data as you normally would in template files
58+
$recent_posts = tpl_data( 'recent_posts' );
59+
foreach( $recent_posts as $post ) {
60+
echo $post->post_title;
61+
}
62+
```
63+
64+
Alternatively, you can call a global to get all of the data available for that template.
65+
66+
```php
67+
// within page.php
68+
69+
global $template_data;
70+
extract( $template_data, EXTR_SKIP );
71+
72+
echo $yo;
73+
74+
foreach( $recent_posts as $post ) {
75+
echo $post->post_title;
76+
}
77+
```
78+
79+
Multiple Controllers
80+
--------------------
81+
82+
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.
83+
84+
Thanks to @DesignPlug for the idea: https://github.com/DesignPlug/wxp-dom-router

‎class-template-controller.php

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<?php
2+
if ( ! class_exists( 'Template_Controller' ) ) {
3+
/**
4+
* Parent class for passing data into template files
5+
*
6+
* All properties in this class are declared statically so that all data from child
7+
* classes can be passed back to this parent class and called directly from here.
8+
* (This takes advantage of a PHP quirk that that within child classes, `self` refers
9+
* to the parent class)
10+
*
11+
* @package WordPress
12+
* @subpackage Template_Controller
13+
* @author Grant Kinney
14+
* @license http://mit-license.org/ MIT
15+
*/
16+
class Template_Controller {
17+
18+
/**
19+
* An array of instances used for singleton pattern that stores an instance for this
20+
* parent class and separate instances for each child class
21+
* @var array
22+
*/
23+
protected static $instances = array();
24+
25+
/**
26+
* An array of body class for the current template
27+
* @var array
28+
*/
29+
protected static $classes = array();
30+
31+
/**
32+
* A key value store for data that is passed into templates
33+
* @var array
34+
*/
35+
protected static $data = array();
36+
37+
/**
38+
* Initialize class
39+
* @return void
40+
*/
41+
public static function init() {
42+
self::get_instance();
43+
}
44+
45+
/**
46+
* Singleton pattern: instantiate one and only one instance of this class, and one and
47+
* only one instance of each child class that extends it
48+
*
49+
* @return object instance of class being instantiated
50+
*/
51+
final public static function get_instance() {
52+
// Note, need PHP 5.3 or greater to use `get_called_class()`
53+
$class = get_called_class();
54+
if ( ! isset( self::$instances[$class] ) ) {
55+
self::$instances[$class] = new $class;
56+
}
57+
return self::$instances[$class];
58+
}
59+
60+
/**
61+
* Hooks for setting up methods within class
62+
*/
63+
public function __construct() {
64+
// Load template data before including template files
65+
add_action( 'template_redirect', array( $this, 'load' ) );
66+
}
67+
68+
/**
69+
* Get a value from the template data by name
70+
*
71+
* @param string $name Name of data stored
72+
* @return mixed Value of data, if it exists, otherwise false
73+
*/
74+
public function get( $name ) {
75+
return isset( self::$data[$name] ) ? self::$data[$name] : false;
76+
}
77+
78+
/**
79+
* Load template data
80+
*
81+
* Loops through each body class for the current template, checks for a method of
82+
* the same name as the class, and calls that method if it exists
83+
*
84+
* @return void
85+
*/
86+
public function load() {
87+
/**
88+
* Global variable to store template data.
89+
*
90+
* Place the following at the top of your template file as an alternative to calling
91+
* `tpl_data()` or `get_tpl_data()` functions for each individual data key
92+
* `global $template_data;`
93+
* `extract( $template_data, EXTR_SKIP );`
94+
*
95+
* @global array template_data
96+
*/
97+
global $template_data;
98+
// Add `common` to class array so that it loads for every template
99+
self::$classes = get_body_class( 'common' );
100+
foreach( self::$classes as $class ) {
101+
$class = str_replace( '-', '_', $class );
102+
if ( method_exists( $this, $class ) ) {
103+
call_user_func( array( $this, $class ) );
104+
}
105+
}
106+
// Push template data out to global variable
107+
$template_data = self::$data;
108+
}
109+
110+
/**
111+
* Add data by name for use in templates
112+
*
113+
* @param string $name Name of data stored
114+
* @param mixed $data Value of data
115+
*/
116+
protected function add( $name, $data ) {
117+
self::$data[$name] = $data;
118+
}
119+
120+
} // end class
121+
} // endif class_exists
122+
123+
/**
124+
* Return a template data value from the specified name
125+
*
126+
* @param string $name Name of data key to retrieve
127+
* @return mixed Data value for use in template
128+
*/
129+
function get_tpl_data( $name ) {
130+
return Template_Controller::get_instance()->get($name);
131+
}
132+
133+
/**
134+
* Echo a template data value from the specified name
135+
*
136+
* @param string $name Name of data key to retrieve
137+
* @return void
138+
*/
139+
function tpl_data( $name ) {
140+
echo Template_Controller::get_instance()->get($name);
141+
}

‎example-controller.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/**
3+
* Controller for template data
4+
* - Each method in the class that matches a template name will be loaded for that template
5+
* - Ex: `single()` will be loaded for single.php. `common()` is called for all templates
6+
* - Within each method, use `$this->add( 'name', $data )` for each variable you'd like to pass to a template
7+
* - Use `tpl_var('name')` or `get_tpl_var('name')` to call the data from within each template file
8+
*
9+
* @package WordPress
10+
* @subpackage Template_Controller
11+
* @uses Template_Controller Parent class for controller
12+
*/
13+
class My_Controller extends Template_Controller {
14+
15+
public function common() {
16+
$this->add( 'hi', 'I load for every template on the site.' );
17+
}
18+
19+
public function page() {
20+
$this->add( 'yo', 'I load for page.php and custom page templates.' );
21+
}
22+
23+
public function single() {
24+
$this->add( 'whazup', 'I load for single.php, for all post types (any template that starts with "single-").' );
25+
}
26+
27+
public function single_post() {
28+
$this->add( 'dude', 'I load only for built in single post templates (aka single-post.php)' );
29+
}
30+
31+
}
32+
33+
// Initialize controller
34+
My_Controller::init();

0 commit comments

Comments
 (0)
Please sign in to comment.