Controllers and Views
Introduction
Following the pattern of Hierarchy of templates from WordPress itself can be seen here, Onyx Theme loads its controllers according to the existence of these files followed by suffix Controller
inside the ./core/app/Controllers
folder.
The controllers are responsible for making the query / queries and loading the Views. You can see the practical examples on the controllers that accompany the theme.
info
In the absence of any controller above the hierarchy, the standard WordPress templates are loaded normally.
By default the theme comes with the following controllers in the PSR4 standard:
- ./core/app/Controllers/
- ArchiveController.php
- CategoryController.php
- Error404Controller.php: Exclusively the 404 controller ignores the standard
- HomeController.php
- PageController.php
- SearchController.php
- SingleController.php
File naming
Following is a table with examples of naming the controllers loaded in order.
The controller files are based on the WordPress hierarchy templates and transformed according to PSR4.
Type | File(s) |
---|---|
Home | HomeController.php IndexController.php |
Single | Single{$PostType}{$Slug}Controller.php Single{$PostType}Controller.php SingleController.php |
Pages | Page{$Slug}Controller.php Page{$ID}Controller.php PageController.php |
Categories | Category{$Slug}Controller.php Category{$ID}Controller.php CategoryController.php ArchiveController.php |
Tags | Tag{$Slug}Controller.php Tag{$ID}Controller.php TagController.php TagController.php ArchiveController.php |
Taxonomies | Taxonomy{$taxonomy}{$term}Controller.php Taxonomy{$taxonomy}Controller.php TaxonomyController.php ArchiveController.php |
Author | Author{$Author}Controller.php Author{$ID}Controller.php AuthorController.php ArchiveController.php |
CPT | Archive{$PostType}Controller.php ArchiveController.php IndexController.php |
note
Tip: You can use var_dump( O::get_hierarchy() );
to check the templates of the current page.
For more information on how the WordPress hierarchy pattern works, visit page-template-files and wphierarchy.
Class naming
Controller classes are named according to PSR4 related to the loaded controller files.
A table with examples of class naming follows.
File | Class |
---|---|
HomeController.php | HomeController |
SingleController.php | SingleController |
SinglePostController.php | SinglePostController |
PageController.php | PageController |
Page{$Slug}Controller.php | Page{Slug}Controller |
CategoryController.php | CategoryController |
Creating a controller
All controllers must extend the main \Onyx\Controller
class and execute the initialize()
method.
Views: Setting the Templates
There are 3 basic ways to call views. All files must be in the ./views/
folder.
Templates are loaded in order of existence in the hierarchy.
Views: Cancel rendering
By default Controllers in Onyx Theme automatically render views. If you want more control over them, we can cancel the render_view ()
method by passing a method in initialize ()
.
Context: Queries
Timber\Twig views work by receiving context variables to render the information on the screen.
You can pass one or several queries to the context if necessary and also use the hook timber/context
to review global application contexts.
In addition to queries, you can pass any type of information along with the context
to Timber. As a custom field with some important information.
info
note
In this example, we do not use the helpers set_context ()
, set_templates ()
or set_{type} _templates ()
and inject the templates and contexts directly into the $templates property.