I use Timber for all my projects, but I would also like to start using a templating language like HAML or Pug. I found a PHP implementation of HAML called MtHaml, which supports twig. I thought this could be great if I could get it to work with Timber.

Unfortunately, while this supports a number of CMS, Wordpress is not on the list. There is a project that brings MtHaml to wordpress, but it's from 7 years ago and I haven't yet figured out how it compiles. It looks like I have to run ./watch from command line to get it to autocompile.

I like Timber because it just compiles on the fly. I drop my twig file in the theme directory and it just works. I would like to do the same thing but with HAML + Twig (Timber). Does anyone know how I would go about this?

Alternatively, I would consider using Twig + Pug but I see even less support for this. Basically I want to get away from the traditional templating system of opening and closing tags, and move to an HTML shorthand


@3x5 "Does anyone know how I would go about this?"

I was in a similar place ~7 years ago when I discovered Twig and was like "how come this isn't in WordPress?????" I saw that there were a few half-hearted projects to port into WP — all of which seemed to be stalled out. So I figured, if I wanted it done, I might have to do it myself.

WP has so many devs/users that if you really drive yourself behind it, you're bound to find a user base. If nothing else, you'll learn a ton about open source, testing, support and how to properly construct an enduring tool.

My tip: if you're ever thinking to yourself: "somebody really should ..." — that somebody is you.

| improve this answer | |
  • Jarred, thanks for the reply, and for creating Timber. I use it all the time. I have actually integrated MtHaml into my theme successfully, so I can now create Wordpress themes with HAML and Twig. However, it's just plain twig. Now, I need to figure out how to get MtHaml to pick up the Timber context. – 3x5 Aug 12 at 19:45
  • MtHaml converts HAML to twig, then lets twig render the result. So I just need to get the twig and pass it to Timber to render, instead of Twig. – 3x5 Aug 12 at 20:01
  • From this example, you'll see at the bottom that you can compile HAML output the resulting, unprocessed twig and save as a variable called $compiled. It works for me, but I would think I could then run Timber::render( $compiled, Timber::get_context() );, rendering $compiled rather than a twig file. This just renders a blank screen, though. – 3x5 Aug 12 at 20:09

OK, I mostly figured this out. This solution assumes you have used composer to install MtHaml into the root of your Wordpress theme. Create a file called index.haml and put your HAML and twig in there, using HAML's twig syntax and all the Timber context you want. Then, put this in index.php:

require __DIR__ . "/autoload.php";
$haml = new MtHaml\Environment('twig', array( 'enable_escaper' => false, , 'enable_dynamic_attrs' => false));
$template = __DIR__ . '/index.haml';
$compiled = $haml->compileString(file_get_contents($template), $template);
Timber::render_string($compiled, Timber::get_context() );

And there you go. MtHaml compiles the .haml file to twig, passes the twig as a string to Timber, and Timber renders the string with Timber context.

This isn't bulletproof yet. I am still having some issues with string interpolation and weird output. For example, in some places, if I have HAML that looks like this:

          = fn('is_category','web') ? '<div class=blue>'

MtHaml compiles it as this:

        <div id="content">
          {% line 31 %}{{ fn('is_category','web') ? '<div class=blue>' }}

It adds the `{% line 31 %} in there. I'm bot sure why, or how to fix that.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.