Skip to content

Use MtHaml with Twig

Nami-Doc edited this page Feb 22, 2013 · 4 revisions

MtHaml offers (currently) two compilations : Twig and PHP. To setup MtHaml to compile Twig, you have two options :

  • Pass it through MtHaml yourself and give it to twig
  • Let MtHaml do itself all the work !

In this short example page, we're obviously going to use the second method. It is taken into account that you required the required files, let's say through Composer.

First, you'll need to create an MtHaml Environment to compile the HAML to Twig code :

$mt = new MtHaml\Environment('twig', array('enable_escaper' => false));

It's important to turn auto-escaping to false (enable_escaper option), else your code is gonna be double-escaped (with Twig's autoescaping strategy).

Let's create a simple Twig Loader, using for example Filesystem.

$fs = new Twig_Loader_Filesystem(array('templates/'));

Now, you have to proxy it through MtHaml's Loader to get auto-compilation to HAML.

$loader = new MtHaml\Support\Twig\Loader($mt, $fs);

This Loader will automatically compile the code to HAML and pass it to Twig. It works with any kind of Loader, as long it's a Twig valid one.

The next step is to actually initialize the Twig Environment, passing it the loader we just instantiated.

$twig = new Twig_Environment($loader, array(
    'cache' => 'cache/',
));

If you have any special setup, just do it here. It's important to note that MtHaml doesn't create a Twig_Environment instance on its own : it's up to you to create one, and do whatever you need with it.

The last step before you can use your lovely MtHaml is to add the Runtime extension. Some operations, such as attributes, require MtHaml to load its own twig extension to work. Just include it like any other extension :

$twig->addExtension(new MtHaml\Support\Twig\Extension());

Ta-da ! Now, you're ready to use MtHaml in your project. Let's recap what we've done :

<?php
// ... some kind of include; system ...

// Haml Parser
$mt = new MtHaml\Environment('twig', array('enable_escaper' => false));

// load HAML files from templates/
$fs = new Twig_Loader_Filesystem(array('templates/'));
// ... but compile them to twig before !
$loader = new MtHaml\Support\Twig\Loader($mt, $fs);

// start twig
$twig = new Twig_Environment($loader, array(
    'cache' => 'cache/',
));
// enable MtHaml Runtime extension
$twig->addExtension(new MtHaml\Support\Twig\Extension());
// ... some twig tweaking to suit your tastes ...

And the last step ... Compiling your file !

Let's start by creating a twig haml file, named my_template.twig in the templates/ directory ...

{% haml %}
!!!
%html
  %head
    %title Success !
  %body
    %h1 Congrats !

Note that this {% haml %} tag is very important : it tells the Haml Loader to actually compile the template to HAML. If you omit it, the file will just be read as a normal Twig template.

Let's compile it now ... How do you do that ? Like any other Twig template ! MtHaml is totally transparent once bootstrapped

echo $twig->render("my_template.twig");

You should get a page named Success with a big Congrats ! title showing up ! Enjoy MtHaml !

Clone this wiki locally