Download the PHP package qtgye/wp-template-wrapper without Composer
On this page you can find all versions of the php package qtgye/wp-template-wrapper. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download qtgye/wp-template-wrapper
More information about qtgye/wp-template-wrapper
Files in qtgye/wp-template-wrapper
Package wp-template-wrapper
Short Description A better way to organize Wordpress templates.
License
Informations about the package wp-template-wrapper
WP Template Wrapper
A better way to develop Wordpress templates.
Features
- Organized Template Files
- Template Based Routing
- Template Wrapper
- Template Includes
- Template Data
- AJAX Routes
Organized Template Files
All the template files reside in the templates
folder.
Any template files at the theme's root folder will not work
Template Slug
Template Wrapper transforms wordpress's default template file naming to provide better organization of the files.
'Slugged' template files, i.e. single-{post_type|post_name|post_id}
etc., are grouped in a subdirectory named to that type.
Example:
templates/single/post.php
will be used in place ofsingle-post.php
.templates/page/about-us.php
will be used in place ofpage-about-us.php
.templates/archive/4.php
will be used in place ofarchive-4.php
.- Template-type files will still be used as is; e.g.,
templates/single.php
,templates/page.php
, etc.
In general, files are organized by {type}/{slug|id|custom-name}.php
format.
Custom Page Templates
Custom page templates should be placed in the templates
folder.
Template Wrapper uses custom page template files only for registration in the admin.
That is, custom page template files will not be used as "views".
You have to create a file of the same name in the templates/page
folder.
Example:
To use a custom "Donate Page Template" using
custom-donate-page.php
,
Create one in thepage-templates
folder as a registry, containing only the template name.
Create another one in thetemplates/page
folder as a "view". This will be the one rendering your view.
Template Based Routing
Template Wrapper allows route registration through template_routes
hook.
Routes are placed in src/routes.php
in the following manner:
$template_slug (String)
The "route" to the template slug. This is similar to the Wordpress's template hierarchy, though transformed to become directory-based.
$callable (Function or Object/Class Method)
The "controller". This will handle the data passed to the template. This can be either a function or an object/class method, similar to MVC approach.
Example:
Note:
A "slugged" route doesnt require a template counterpart. Just like wordpress's template hierarchy, it will use the available template for that slug.
That is, a page/about
route will use templates/page.php
if templates/page/about.php
is not available.
Template Wrapper
This is inspired by scribu's theme wrapper which also implemented in Sage starter themes.
It allows you to have your main layout in wrapper.php
, and the main template will be automatically wrapped with it.
It prevents code repition caused by using get_header()
or get_footer()
at each main template.
Template Includes
The Template Wrapper provides a way to include templates and passing data through include_template
method of the Wrapper
class.
To avoid having to declare Wrapper's namespace in every template, it is recommended to wrap it in a global function instead, like so:
$template_slug (String)
The slug of the template file relative to the templates folder. In the example, it will load
templates/modules/content.php
.
$data (Assoc. Array)
The data to be passed into the included template. Note that user-defined globals are also available in the included templates, so if there are common variable names, the template data will override it.
Template Data
The route callable handles the data being passed to the template. Through reflection, the callable may receive user-defined globals as arguments.
Example:
Global Data
The user-defined global data is defined in the src/global.php
through the template_global_data
hook. These globals are available within templates.
Example:
Template Wrapper provides pre-defined globals to which user-defined globals are merged:
$wp, $wpdb, $wp_query, $post, $authordata, $page;
AJAX Routes
Template Wrapper provides a way to handle ajax routing using action.
Register ajax routes through the ajax_routes
hook.
Within the ajax callback, GET parameters and user-defined globals are passed through reflection.
TODOs:
- [] Multiple wrappers