Download the PHP package aaronkirkham/wp-timber-view-controller without Composer
On this page you can find all versions of the php package aaronkirkham/wp-timber-view-controller. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download aaronkirkham/wp-timber-view-controller
More information about aaronkirkham/wp-timber-view-controller
Files in aaronkirkham/wp-timber-view-controller
Package wp-timber-view-controller
Short Description A view controller for using Timber on WordPress
License MIT
Homepage https://kirkh.am
Informations about the package wp-timber-view-controller
WordPress Timber View Controller
If you're like me and hate writing 2 files to render a twig template for WordPress (e.g. page.php for context and page.twig for markup) then this library is for you. It follows the WordPress Template Hierarchy so you can also create files such as single-{post_type}.twig
.
How to use
Using wp-timber-view-controller is easy.
composer require aaronkirkham/wp-timber-view-controller
Then place the following code inside your themes functions.php:
and that's it. Your Twig templates will be automatically rendered.
This library uses the internal Timber::$dirname
variable to locate your templates (default folder is views
). If you want your files to live in a different folder, you must overwrite this.
If you need to add variables to the Timber context, there are handy filters available for that.
Filter: timber_context--%TEMPLATE%
The timber_context--%TEMPLATE%
filter (where %TEMPLATE% is the current template name) is fired when the specific template is rendered. This is handy if you want to get posts from WordPress on specific pages.
In the above examples, the message variable will be available on all pages which are rendered using the 404.twig template, and the post variable will be available on all single.twig templates.
FYI
If you want to add data into the context for every template, you should use the timber_context
filter which is fired by Timber.
The variable foo is now available on all templates.
White screen?
If your twig templates are not rendered or you just have a white screen, you should turn on WP_DEBUG inside wp-config.php to view a hierarchical list of templates that wp-timber-view-controller tried to render. Double check the displayed template path and file names are correct.