Haml (HTML Abstraction Markup Language) is a templating system that is designed to avoid writing inline code in a web document and make the
HTML
Hypertext Markup Language (HTML) is the standard markup language for documents designed to be displayed in a web browser. It defines the content and structure of web content. It is often assisted by technologies such as Cascading Style Sheets ( ...
cleaner. Similar to other 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 a extension. These files are similar to .erb or .eRuby files, which also help embed
Ruby
Ruby is a pinkish-red-to-blood-red-colored gemstone, a variety of the mineral corundum ( aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called sapph ...
code while developing a
web application
A web application (or web app) is application software that is created with web technologies and runs via a web browser. Web applications emerged during the late 1990s and allowed for the server to dynamically build a response to the request, ...
.
While parsing code comments, Haml uses the same rules as Ruby 1.9 or later. Haml understands only
ASCII
ASCII ( ), an acronym for American Standard Code for Information Interchange, is a character encoding standard for representing a particular set of 95 (English language focused) printable character, printable and 33 control character, control c ...
-compatible encodings, like
UTF-8
UTF-8 is a character encoding standard used for electronic communication. Defined by the Unicode Standard, the name is derived from ''Unicode Transformation Format 8-bit''. Almost every webpage is transmitted as UTF-8.
UTF-8 supports all 1,112,0 ...
, but not
UTF-16
UTF-16 (16-bit Unicode Transformation Format) is a character encoding that supports all 1,112,064 valid code points of Unicode. The encoding is variable-length as code points are encoded with one or two ''code units''. UTF-16 arose from an earli ...
, or UTF-32, because these are not compatible with
ASCII
ASCII ( ), an acronym for American Standard Code for Information Interchange, is a character encoding standard for representing a particular set of 95 (English language focused) printable character, printable and 33 control character, control c ...
.
Haml can be used at the
command line
A command-line interface (CLI) is a means of interacting with software via command (computing), commands each formatted as a line of text. Command-line interfaces emerged in the mid-1960s, on computer terminals, as an interactive and more user ...
, as a separate Ruby module, or in a Ruby on Rails application.
History
Haml was originally introduced by Hampton Catlin with its initial release in 2006 and his work was taken up by a few other people. His motive was to make
HTML
Hypertext Markup Language (HTML) is the standard markup language for documents designed to be displayed in a web browser. It defines the content and structure of web content. It is often assisted by technologies such as Cascading Style Sheets ( ...
simpler, cleaner, and easier to use. Since 2006, it has been revised several times, and newer versions have been 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
Ruby is a pinkish-red-to-blood-red-colored gemstone, a variety of the mineral corundum ( aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called sapph ...
applications, while the branding and design were done by Nick Walsh.
Version 2.2.0 was released in July 2009 with support for Ruby 1.9 and
Rails
Rail or rails may refer to:
Rail transport
*Rail transport and related matters
* Railway track or railway lines, the running surface of a railway
Arts and media Film
* ''Rails'' (film), a 1929 Italian film by Mario Camerini
* ''Rail'' (1967 fi ...
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.
Examples
Haml markup is similar to CSS in syntax. For example, Haml has the same dot . representation for classes as CSS does.
The following are equivalent as HAML recognises CSS selectors:
%p Hello, World!
%p.sample#welcome Hello, World!
These render to the following
HTML
Hypertext Markup Language (HTML) is the standard markup language for documents designed to be displayed in a web browser. It defines the content and structure of web content. It is often assisted by technologies such as Cascading Style Sheets ( ...
Haml can be integrated into Ruby on Rails as a plugin. Similar to eRuby, Haml also can access local variables (declared within same file in Ruby code). This example uses a sample Ruby controller file.
* file: app/controllers/messages_controller.rb
class MessagesController < ApplicationController
def index
@message = "Hello, World!"
end
end
* file: app/views/messages/index.html.haml
#welcome
%p= @message
This renders to:
Hello, World!
Haml as a Ruby module
Haml is also capable of being used independently as a Ruby
library
A library is a collection of Book, books, and possibly other Document, materials and Media (communication), media, that is accessible for use by its members and members of allied institutions. Libraries provide physical (hard copies) or electron ...
Haml uses whitespace indentation (two spaces) for tag nesting and scope, replacing open-end tag pairs. The following example compares the syntaxes of Haml and eRuby (Embedded Ruby), alongside the HTML output.
Key differences are:
* Haml doesn't have both opening and closing tags for each element like eRuby.
* eRuby syntax looks a lot like
HTML
Hypertext Markup Language (HTML) is the standard markup language for documents designed to be displayed in a web browser. It defines the content and structure of web content. It is often assisted by technologies such as Cascading Style Sheets ( ...
and is thereby more HTML-like while Haml is more CSS-like.
* Haml uses
indentation
__FORCETOC__
In the written form of many languages, indentation describes empty space ( white space) used before or around text to signify an important aspect of the text such as:
* Beginning of a paragraph
* Hierarchy subordinate concept
* Qu ...
to nest tag elements whereas eRuby uses the same HTML representation.
* In Haml properties like class, id can be represented by ., # respectively instead of regular class and id keywords. Haml also uses % to indicate a HTML element instead of <> as in eRuby.
XHTML
Extensible HyperText Markup Language (XHTML) is part of the family of XML markup languages which mirrors or extends versions of the widely used HyperText Markup Language (HTML), the language in which Web pages are formulated.
While HTML, pr ...
:
BoBlog
BoBlog
Bob's Blog
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 new Templating Engine out for Ruby on Rails. It's called Haml.
Implementations
The official implementation of Haml has been built for
Ruby
Ruby is a pinkish-red-to-blood-red-colored gemstone, a variety of the mineral corundum ( aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called sapph ...
BBCode
BBCode ("Bulletin Board Code") is a lightweight markup language used to format messages in many Internet forum software. It was first introduced in 1998. The available "tags" of BBCode are usually indicated by square brackets ( and ">/code> and ...
Ruby
Ruby is a pinkish-red-to-blood-red-colored gemstone, a variety of the mineral corundum ( aluminium oxide). Ruby is one of the most popular traditional jewelry gems and is very durable. Other varieties of gem-quality corundum are called sapph ...
* Ruby on Rails
* YAML
* Sass – a similar system for CSS, also designed by Catlin.
* Website Meta Language – another template language with similar functionalities
*
Web template
A web template system in web publishing allows web designers and developers to work with ''web templates'' to automatically generate custom web pages, such as the results from a search. This reuses static web page elements while defining dynam ...
– general concept of template to
HTML
Hypertext Markup Language (HTML) is the standard markup language for documents designed to be displayed in a web browser. It defines the content and structure of web content. It is often assisted by technologies such as Cascading Style Sheets ( ...