Haml


Haml is a templating system that is designed to avoid writing inline code in a web document and make the HTML cleaner. Haml gives the flexibility to have some dynamic content in HTML. Similar to other web languages like PHP, ASP, JSP and template systems like eRuby, Haml also embeds some code that gets executed during runtime and generates HTML code in order to provide some dynamic content. In order to run Haml code, files need to have.haml extension. These files are similar to.erb or eRuby files which also help to embed Ruby code while developing a web application.
While parsing coding comments, Haml uses the same rules as Ruby 1.9 or later. Haml understands only ASCII compatible encodings like UTF-8 but not UTF-16 or UTF-32 because these are not compatible with ASCII.
Haml can be used in command line, as a separate Ruby module, or in a Ruby on Rails application making Haml suitable for a wide range of applications.

History

Haml was originally introduced by Hampton Catlin with its initial release in 2006 and his work was taken ahead by a few other people. His motive was to make HTML simpler, cleaner and easier to use. Since 2006, it has been revised several times and newer versions were released. Until 2012 Natalie Weizenbaum was the primary maintainer of Haml, followed by Norman Clarke until 2015. Natalie worked on making Haml usable in Ruby applications, while the branding and design was done by Nick Walsh. Others who are currently in the maintenance team are Matt Wildig, Akira Matsuda, Tee Parham and Takashi Kokubun.

Version history

Version 2.2.0 was released in July 2009 with support for Ruby 1.9 and Rails 2.0 or above. Version 3.0.0 was released in May 2010, adding support for Rails 3 and some performance improvements. The fourth major version broke compatibility with previous versions, only supporting Rails 3 and Ruby 1.8.7 or above, and marked the switch to semantic versioning. Several amendments like increasing the performance, fixing a few warnings, compatibility with latest versions of Rails, fixes in the documentation and many more were made in the Haml 4 series. Version 5.0.0 was released in April 2017. It supports Ruby 2.0.0 or above and drops compatibility with Rails 3. A 'trace' option, which helps users to perform tracing on Haml template, has been added.

Features

Four principles were involved in development of Haml.

User-friendly markup

is user-friendly if it adheres to following features:
Markup language should adhere to the Don't repeat yourself principle. It should:
Markup language with good indentation improves appearance, makes it easy to read for readers and also to determine where a given element starts and ends.

Clear structure

Markup language with a clear structure will help in code maintenance and logical understanding of final result. It is unclear whether Haml offers any differential advantage in this regard.

Examples

Haml markup is similar to CSS in syntax. For example, Haml has the same dot . representation for classes as CSS does, making it easy for developers to use this markup.

"Hello, World!"

Haml as a command-line tool">Command-line interface">command-line tool

The following are equivalent as HAML recognises CSS selectors:

%p Hello, World!


%p.sample#welcome Hello, World!

These render to the following HTML code:

Hello, World!


Haml as an add-on for [Ruby on Rails]

To use Haml with Ruby, the Ruby Gemfile should include this line:
gem 'haml'
Similar to eRuby, Haml also can access local variables. This example uses a sample Ruby controller file.
class MessagesController < ApplicationController
def index
@message = "Hello, World!"
end
end
  1. welcome
%p= @message

This renders to:


Hello, World!



Haml as a Ruby module

To use Haml independent of Rails and , install haml gem, include it in Gemfile and simply import it in Ruby script or invoke Ruby interpreter with -rubygems flag.

welcome = Haml::Engine.new
welcome.render

Output:

Hello, World!



is a Haml class.

Basic example

Haml uses whitespace indentation for tag nesting and scope. This acts as a replacement for the open-end tag pairs, making it DRY and cleaner. The following example compares the syntaxes of Haml and eRuby, alongside the HTML output.
Key differences are:
Note: This is a simple preview example and may not reflect the current version of the language.

!!!
%html
%head
%title BoBlog
%meta
%link
%body
#header
%h1 BoBlog
%h2 Bob's Blog
#content
- @entries.each do |entry|
.entry
%h3.title= entry.title
%p.date= entry.posted.strftime
%p.body= entry.body
#footer
%p
All content copyright © Bob

The above Haml would produce this XHTML:

XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


BoBlog







Halloween

Tuesday, October 31, 2006



Happy Halloween, glorious readers! I'm going to a party this evening... I'm very excited.




New Rails Templating Engine

Friday, August 11, 2006



There's a very cool new Templating Engine out for Ruby on Rails. It's called Haml.







Implementations

The official implementation of Haml has been built for Ruby with plugins for Ruby on Rails and Merb, but the Ruby implementation also functions independently. Haml can be easily used along with other languages. Below is a list of languages in which Haml has implementations: