Haml for Django developers

Haml is taking the Ruby(and Rails) world by storm. Its not used as heavily by Python(and Django) developers as the Python solutions aren’t as mature. I finally gave Haml a try and was pleasantly surprised how easy it was.

The most mature Python implementation of Haml is hamlpy, which converts the hamlpy code to Django templates. Others are shpaml and GHRML

Lets look at some templates from Django tutorial

Here is this template converted to Haml

-if latest_poll_list
 %ul
  -for poll in latest_poll_list
    %li
        %a{'href':'/polls/'}= poll.question

You can see that this lost a lot of noise from the Django template. Being a Django programmer, significant whitespace is very pleasing.

Here is another one from the same tutorial.

<h1></h1>
<ul>

</ul>

And converted to hamlpy

%h1
    = poll.question
%ul
    -for choice in poll.choice_set.all
        %li
            = choice.choice

Again, I lose a lot of noise from the templates, and the signifiant whitespace improve the chance that the out put would be valid html.

Here are a few more templates.

In particular see this which is haml for this Pinax file


Resource

  1. Haml
  2. Hamlpy

Thank you for reading the Agiliq blog. This article was written by shabda on Dec 25, 2011 in django .

You can subscribe ⚛ to our blog.

We love building amazing apps for web and mobile for our clients. If you are looking for development help, contact us today ✉.

Would you like to download 10+ free Django and Python books? Get them here