Haml: Markup Haiku

By Adeel Khan (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):

Download code
!!! 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):

Download code
.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 472

CakePHP Team Comments Author Comments
 

Comment

1 Used in Ruby on Rails

I have used haml with ruby on rails. I am not sure how viable of a solution this would be for Cake though.
Posted Jul 19, 2007 by Jonathan Bradley
 

Comment

2 Being implemented in various languages

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 Jan 25, 2008 by Dizzle
 

Question

3 Does this include Sass as well

And can it parse external stylesheets coded in Sass for the views? Thank you.
Posted Feb 24, 2008 by Aeron Glemann