Haml

From Wikipedia, the free encyclopedia

Haml
Image:Haml-logo.gif
Developer: Hampton Catlin, Nathan Weizenbaum
Latest release: 1.5.1 / March 31, 2007
OS: Cross-platform
Use: Template Engine
License: MIT License
Website: haml.hamptoncatlin.com

Haml (XHTML Abstraction Markup Language) is a markup language that is used to cleanly and simply describe the XHTML of any web document without the use of traditional inline coding. It’s designed to address many of the flaws in traditional templating engines, as well as making markup as elegant as it can be. Haml functions as a replacement for inline page templating systems such PHP, RHTML, and ASP. However, Haml avoids the need for explicitly coding XHTML into the template, because it iself is a description of the XHTML, with some code to generate dynamic content.

Contents

[edit] Principles

[edit] Markup should be beautiful

Markup should not be used merely as a tool to get browsers to render a page how the author wants it rendered. The rendering isn't the only thing people have to see; they have to see, modify, and understand the markup as well. Thus, the markup should be just as user-friendly and pleasant as the rendered result.

[edit] Markup should be DRY

XHTML involves major repetition. Every element is named twice: once before its content and once after. ERB adds even more repetition and unnecessary characters. HAML avoids all of this by relying on indentation, not text, to determine where elements and blocks of code begin and end. Not only does this result in smaller templates, it makes the code much cleaner to look at.

[edit] Markup should be well-indented

One of the major problems with traditional templating languages is that not only do they not encourage well-indented code, they actively make it challenging, or even impossible, to write. This leads to confusing, unreadable XHTML, when it should be well indented and reflecting the underlying structure of the document. Haml automatically properly formats all tags so that it does.

[edit] XHTML structure should be clear

XML and XHTML are formats built upon the idea of a structured document. That structure is reflected in their markup, and it should likewise be reflected in meta-markup such as Haml Because Haml's logic is based on indentation of child elements, this structure is naturally preserved, making the document much easier and more logical for mere humans to read.

[edit] Disadvantages

  • Being "whitespace active", which requires 2 spaces for each indent level, requires that text editors are properly configured to use the tab key.

[edit] Example

Note: This is a simple preview example and may not reflect the current version of the language.

!!!
%html{ :xmlns => "http://www.w3.org/1999/xhtml", :lang => "en", 'xml:lang' => "en" }
  %head
    %title BoBlog
    %meta{ 'http-equiv' => 'Content-Type', :content => 'text/html; charset=utf-8' }/
    = stylesheet_link_tag 'main'
  %body
    #header
      %h1 BoBlog
      %h2 Bob's Blog
    #content
      - @entries.each do |entry|
        .entry
          %h3.title= entry.title
          %p.date= entry.posted.strftime("%A, %B %d, %Y")
          %p.body= entry.body
    #footer
      %p
        All content copyright © Bob

The above HAML would produce this XHTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang='en' xml:lang='en' xmlns='http://www.w3.org/1999/xhtml'>
  <head>
    <title>BoBlog</title>
    <meta content='text/html; charset=utf-8' http-equiv='Content-Type' />
    <link href="/stylesheets/main.css?" media="screen" rel="Stylesheet" type="text/css" />
  </head>
  <body>
    <div id='header'>
      <h1>BoBlog</h1>
      <h2>Bob's Blog</h2>
    </div>
    <div id='content'>
      <div class='entry'>
        <h3 class='title'>Halloween</h3>
        <p class='date'>Tuesday, October 31, 2006</p>
        <p class='body'>
          Happy Halloween, glorious readers! I'm going to a party this evening... I'm very excited.
        </p>
      </div>
      <div class='entry'>
        <h3>New Rails Templating Engine</h3>
        <p class='date'>Friday, August 11, 2006</p>
        <p class='body'>
          There's a very cool new Templating Engine out for Ruby on Rails. It's called HAML.
        </p>
      </div>
    </div>
    <div id='footer'>
      <p>
        All content copyright © Bob
      </p>
    </div>
  </body>
</html>

[edit] Implementations

The only current implementation of Haml has been built for Ruby with plugins for Ruby on Rails, but the language itself could be extended for any other framework or language.

[edit] Development

Haml was invented in May 2006 by Hampton Catlin who continues to work on the implementation and the ideals behind Haml. However, Nathan Weizenbaum is responsible for much of the recent growth and maturation of the Haml codebase.


[edit] Current Usage

Haml is currently being used in several open source projects including How'd They Vote and Contentopolis. Also, it is the primary template language on many closed-source websites.

[edit] See also

[edit] External links

In other languages