Welcome to Haanga's home

 

Haanga is a template engine that uses Django syntax. In the beginning it was developed for Menéame, but we decided to release it as an independent project so it can be used by anyone :-)

Some Hangaa’s features:

  • Human readable.
  • Easy to use and mantain.
  • Highly efficient because every template is “compiled” into a PHP script.
  • The generated code is ready to take advantage from any PHP optimizer (like xcache).

Why a new template engine?

  • At Meneame performance is prioritary.
    1. The compilation is only made in the worst case (when there is no template or the template is newer than the generated)
    2. Usually, only a small piece of Haanga is loaded –the one that checks and determinates if it’s necessary a compilation.
    3. Generated code is optimized.
      1. One function is generated.
      2. Everything is PHP (no context changes between HTML and PHP)
  • Django’s syntax is beautiful :-)

Beginnings of the project

It all started with a tweet, Meneame (an open source digg-like site from Spain) was looking for an efficient template engine.

A few days later I began with some outlines for the lexer, the parser and the rudimentary code generator (all the prototype record is public)

The name of the project it’s from a Guarani word –Ha’anga- which means “draw” or “shape”

Using Haanga

In order to use Haanga into any project, after install, you must configure it. A basic configuration looks like this:

<?php

require "lib/Haanga.php";

Haanga::configure(array(
    'template_dir' => '../templates/',
    'cache_dir' => '../compiled/',
));

Suppose we want a template which prints a given username and a URL if it is defined:

<h1>
    Hi there, I'm {{user}}
    {% if url %}
        and my URL is <a href="{{url|safe}}">{{url}}</a>
    {% endif %}
</h1>

Now, let's load the template from PHP:

<?php
$user = 'crodas';
$url  = 'http://meneame.net/user/crodas';
$vars = compact('user', 'url');

Haanga::Load('sample_template.html', $vars);

Who are behind Haanga?