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
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
Share Improve this question asked Aug 10, 2020 at 2:28 3x53x5 1335 bronze badges2 Answers
Reset to default 0@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.
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:
#content
= 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.