Nicer Templates with HAML in Ruby or PHP
Writing templates for a browser based app does alway mean to mix HTML with you preferred Scripting language. The beauty of such code differs, depending on the template language used, but it never reaches the beauty of pure scripting language codeblocks in your models or controllers.
This fact could change with a new markup approach called HAML – HTML Abstraction Markup Language. To quote the Author of HAML, Hampton Catlin:
Haml is a refreshing take that is meant to free us from the shitty templating languages we have gotten used to.
But why are the traditional approaches we all use so ugly? Well take a look at this:
<html>
<head>
<?php if ($title) { ?>
<title><?php echo $title; ?></title>
<?php } else { ?>
<title><?php echo $pagename; ?></title>
<?php } ?>
</head>
<body>
<div id=”header”>
<h1><?php echo $pagename; ?></h1>
<?php if ($slogan) { ?>
<span><?php echo $slogan; ?></span>
<?php } ?>
</div>
Transfered into HAML Markup it looks like this:
%html
%head
- if ( $title)
%title= $title
- else
%title= $pagename
%body
#header
%h1 Example page
- if ($slogan)
%span= $slogan
HAML was initially coded for Ruby and Amadeusz Jasak ported it to php 5 called phpHAML.
This is the most readable approach i have seen so far and i definitely need to give it a try in my cakePHP projects. There is already a tutorial in the CakeBakery covering the implementation.

