Haml: Markup Haiku

This article is also available in the following languages:
By chess64
Haml takes your gross, ugly templates and replaces them with veritable Haiku. Haml is based on one primary principal: markup should be beautiful. Check out http://haml.hamptoncatlin.com/.
This is a guide to using Haml in CakePHP instead of the normal .ctp/.thtml files. Note: this uses phphaml which is for PHP5.

First of all, download the HamlParser and HamlView from http://cakeforge.org/snippet/detail.php?type=package&id=35. Place HamlParser.class.php in the app/vendors/haml/ directory and place haml.php in the app/views/ directory. Create the directory app/tmp/haml and make it world-writable (chmod 0777). This is where the compiled php files are placed. Next, in your AppController, put var $view = 'Haml';. Now all view files with the extension '.haml' will be parsed as Haml. That's it!

Here's a sample Haml layout (app/views/layouts/default.haml):


!!! Strict
%html
  %head
    %meta{ :http-equiv => 'Content-Type', :content => 'text/html;charset=utf-8' }
    %title= $title_for_layout
  %body
    #header
      %h1 hello
    #content= $content_for_layout
    #footer
      %span.author John Q. Caker

And here's a sample view (app/views/hello/index.haml):


.hello
  %p Hello, world!
  = $html->link('This is a link to the home page', '/')

As you can see, using helpers inside Haml is no trouble at all.

Comments

  • Posted 12/17/09 03:47:36 AM
    HamlParser:228:
    "while(ob_get_level() > 0) {
    ob_end_flush();
    }"
  • Posted 02/24/08 10:41:19 AM
    And can it parse external stylesheets coded in Sass for the views? Thank you.
  • Posted 01/25/08 05:14:31 AM
    HAML is being actively developed in various lanaguages, for various technologies:

    - ASP.NET MVC Framework (NHAML)
    - Ruby (origin)
    - Multiple implementations for PHP (PHPHAML on SF (as above) & pHAML)

    If you're interested in the 'viability', or rather, how it can help you, check out the HAML entry on Wikipedia, that will help answer your questions. And, as always, TMTOWTDI.
  • Posted 07/19/07 01:50:27 PM
    I have used haml with ruby on rails. I am not sure how viable of a solution this would be for Cake though.

Comments are closed for articles over a year old