Roots + Timber


#1

Hi All,

Anybody out there using Timber?

http://jarednova.github.io/timber/

In a previous life I used a similar template language and really enjoyed it.

I’m wondering about using Timber with Roots.

Anybody tried it yet?

Rob


Using Timber + Twig with Sage , Bedrock, Trellis
Templating engine in Sage
Timber WP plugin
#2

I haven’t tried it, but i Have heard good things about Twig. Seems like it could be helpful.

Might be worth trying on a project. It does make using PHP more pretty, that’s for sure.


#3

I’m all for using a real templating language (which PHP isn’t) and Twig is great. I even wrote an experimental WP theme using it as well: https://github.com/swalkinshaw/radicle

Problem with Timber is in order to really implement Twig, you need to rewrite tons of WP functionality to expose it to Twig. So now you’re also relying on that library not having bugs (in addition to WP) and learning their way of doings things as well.


#4

I know this is an old issue, but I’ve also been thinking about this…
I wanted to try using twig for my shoestrap theme, but I do want to still be “compatible” with Roots (since this is a branch of the rootstheme), so I wouldn’t really consider it as an option if Roots is not using it as well.
Would you guys be interested if I fork Roots and rewrite it using twig?


#5

For the reasons @swalkinshaw mentioned above, we’re not interested in integrating Twig at this time


#6

great, thanks for the update!


#7

Is this still out of the question? I’m working on a new theme based on Roots and I’d like to use Twig as well, so to start, I would pretty much be developing a Roots + Timber fork anyway. Would you be interested in a pull request or should I keep this as a personal project?


#8

You can post it on Github, I’d like to take a look. However, for all the things that Roots does a little differently from many starter themes, switching to Twig is a pretty far departure from WordPress, and would actually require a plugin dependency that changes a whole lot. So I don’t think it will ever be incorporated into the main theme.

But again, I would love for WP to start using a templating engine one day, so I’d like to see someone’s version of Roots + Timber :slight_smile:


#9

Thanks for the input! I’ll submit something then.

True. Requiring a dependency isn’t the best idea for Roots I suppose. In the two projects I’ve used Timber for, I’ve either just downloaded the plugin or loaded it as a Git submodule and included it through functions.php. So that’s how I’m going to approach this one too.


#10

Any reason why you aren’t using Composer?


#11

I know I’m digging up an old topic, but I’ve been using timber/twig to extend an older heavily customized Roots theme I inherited as part of my job.

I ran into a couple small gotchas when I first rolled it out, but it has worked out really well for me. I’m able to roll out new templates super easily, reuse templates and sub-templates all over the place, and simplify working with very complex ACF field groups.

I wouldn’t have started with Roots honestly, but that’s what I inherited. But Timber/twig has been a fantastic addition to it.

The use case that won me over was creating a modular “page builder” that used 100% structured content stored in the wp post meta fields. ACF flex content and various repeaters were already in use - so I extended them, and stuck timber on the front end to render out the content. This took like a day to build, and works extremely well. It replaced a very wonky Themeforest pagebuilder plugin.

I like that I can build the data object any which way on the php side - do things that are tricky to do with nested loops and taxonomy queries, and end up with exactly the data layout I need for my template. This is so much easier for doing things like building complex “fat” menus that include dynamic content - way way way easier than building some hairy menu walker and figuring out how to inject posts into it. :slight_smile:

At some point I need to rebuild the theme completely, and I am not sure I’ll stick with Roots(now Sage I guess) - but this is definitely not a knock against Roots. Far from it - there’s lots of good ideas in the theme that are applicable to working with Timber. I also need to figure out how to integrate Composer into my workflow.

I think there’s room for both Timber and Roots - and I’m not suggesting the Roots team adopt Timber. Just a thumbs up from someone using both together.


#12

Just curious, what would you use instead?


#13

Have you looked into Bedrock?

There’s also a screencast on Using Composer with WordPress available from one of the Roots developers which might help you out.


#14

There’s a couple Timber starter themes that use very similar principles to roots, and follow the same DRY principles. The only reason I’d skip using it if I rebuild the theme is that Timber provides many of the same features. Since I’m using timber, it makes more sense to take advantage of its own modular & wrapper features. Bedrock might actually be an ok fit to start - haven’t used it yet, but i’ll spend some time checking it out. And just to be clear - this is for one specific project. There’s other projects where Sage / Roots would be a better fit.

The router portion of timber is pretty cool too - its something I’ve just started playing with and it makes mapping complex queries to nice urls very easy.

I will say that Roots / Sage has great documentation and likely a bigger community. :smile:


#15

I haven’t actually used Timber but used twig for a couple of years. Indeed, a lot of the ‘smart’ bits of Sage are simply how you use Twig (partials, theme wrapper…).

Ultimately though, I agree with this sentiment:

I do hope that it will occasionally be revisited by the Roots team though. I think the projects are very philosophically compatible.


#16

I made a Twig version of the Sage theme. I’ve used the Timber plugin and Sage theme in a couple of projects so far and extremely happy with both.

You can find it at: https://github.com/studiorabota/sage-twig-theme.


#17

Thanksl! I’ve started playing with Timber & Twig too in my latest project.

As an addition, if you want to keep using the sidebar check function of Sage, you can add the following in your functions.php:

use Roots\Sage\Setup;

class StarterSite extends TimberSite {
	function __construct() {
		add_filter( 'timber_context', array( $this, 'add_to_context' ) );
		parent::__construct();
	}
	function add_to_context( $context ) {
		$context['main_menu']  = new TimberMenu('primary_navigation');
		$context['site']       = $this;
		$context['display_sidebar']    = Setup\display_sidebar();
		$context['sidebar_primary'] = Timber::get_widgets('sidebar-primary');
		return $context;
	}
}
new StarterSite();

And use it in your base.twig:

<div class="wrap container">
  <div class="content row">
    <main class="main">
      {% block content %}No block content{% endblock %}
    </main>
    {% if display_sidebar %}
      <aside class="sidebar">
        {% block sidebar %}
          {% include 'sidebar.twig' %}
        {% endblock %}
      </aside>
    {% endif %}
  </div>
</div>

and in sidebar.twig:

{% block sidebar %}
  {{sidebar_primary}}
{% endblock %}

#18

Thank you Twansparant! I’ve added your code to the Git repository.


#19

Just released the latest version of Sage 8.5.1 port to Timber (twig).