Timber, Sage, and the Templates Folder

I was looking at this issue on Sage’s GitHub issues:

I definitely love this idea, and I’ve been thinking a bit about how to do this combined with Timber. What I’d really like to do is eliminate any requirement to have any PHP template files scattered around. If you look at the Timber starter theme, the way they do it is to drop the Timber initialization functions into the regular WordPress templates (i.e. single.php has the Timber::get_context() and Timber:render() functions, with there being a separate templates folder that has the actual Twig templates.

I’m thinking about using the idea behind the Sage wrapper and treating it kind of like “router” that decides which templates to render and the logic for passing the data, so you don’t need all these extra files scattered in your theme root folder and you get a lot of the benefits of Sage (i.e. a “base” layout all the other layouts inherit from).

At this point, I’m mostly bouncing the ideas around in my head, but would love to throw it out to the Roots community to see if anyone has any thoughts about the approach and/or if anyone is interested in working with me on this.

1 Like

The issue with removing files from the WordPress Template Hierarchy is that its routing is entirely dependent on the existence of those files.

What you could do is set up routing in the more generic template pages. So you define the routes for all pages in page.php all the routes for the singles in single.php etc etc.

If I understand it correctly, the WordPress routing determines which template to load. I’m thinking hook into that, similar to the way the wrapper does, to load a Twig template instead of a PHP file, but I’m not sure how I would structure that.

What I was trying to say is that the PHP file must exist in order to be found by the template hierarchy, so you would still need .php files and .twig files.

It’s the same issue we encounter with the wrapper. base-page-{id}.php will not load for a page matching the id unless page-id.php also exists.

1 Like

Oh I see… yeah, it looks like there is a filter for hooking into these things, but we’d basically have to recreate the logic of locate_template ourselves for the twig templates.

Also feel free to comment here :wink:

1 Like

I would love to see this merged as part of core, but it’s literally going to take years. I’d love to start thinking about how we might implement this on our own.