Haml
Encyclopedia
Haml is a lightweight markup language
that is used to 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 as PHP
, RHTML, and ASP
. However, Haml avoids the need for explicitly coding XHTML into the template, because it is itself a description of the XHTML, with some code to generate dynamic content.
Haml's equivalent for CSS
is Sass.
Markup should be DRY
Markup should be well-indented
XHTML structure should be clear
The above Haml would produce this XHTML:
with plugins for Ruby on Rails
and Merb
, but the Ruby implementation also functions independently.
There are also implementations in other languages:
Lightweight markup language
A lightweight markup language is a markup language with a simple syntax, designed to be easy for a human to enter with a simple text editor, and easy to read in its raw form....
that is used to describe the XHTML
XHTML
XHTML is a family of XML markup languages that mirror or extend versions of the widely-used Hypertext Markup Language , the language in which web pages are written....
of any web document
Web document
A web document is similar in concept to a web page, but also satisfies the following broader definition:The term "web document" has been used as a fuzzy term in many sources A web document is similar in concept to a web page, but also satisfies the following broader (W3C) definition:The term "web...
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 as PHP
PHP
PHP is a general-purpose server-side scripting language originally designed for web development to produce dynamic web pages. For this purpose, PHP code is embedded into the HTML source document and interpreted by a web server with a PHP processor module, which generates the web page document...
, RHTML, and ASP
Active Server Pages
Active Server Pages , also known as Classic ASP or ASP Classic, was Microsoft's first server-side script engine for dynamically-generated Web pages. Initially released as an add-on to Internet Information Services via the Windows NT 4.0 Option Pack Active Server Pages (ASP), also known as Classic...
. However, Haml avoids the need for explicitly coding XHTML into the template, because it is itself a description of the XHTML, with some code to generate dynamic content.
Haml's equivalent for CSS
CSS
-Computing:*Cascading Style Sheets, a language used to describe the style of document presentations in web development*Central Structure Store in the PHIGS 3D API*Closed source software, software that is not distributed with source code...
is Sass.
Principles
Markup should be beautiful- Markup should not be used merely as a tool to get browsers to render a page the way its 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.
Markup should be DRY
Dry
Dry or dryness may refer to:* Lack of water* Prohibiting alcohol * Dryness , the lack of sugar in a drink, especially an alcoholic one * Dryness * Dryness...
- XHTML involves major repetition. Most elements have to be named twice: once before its content and once after. ERBERubyeRuby is a templating system that embeds Ruby into a text document. It is often used to embed Ruby code in an HTML document, similar to ASP, JSP and PHP.-Usage:eRuby allows Ruby code to be embedded within a pair of delimiters...
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.
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. The result is confusing, unreadable XHTML. Haml formats tags so they are well indented and reflect the underlying structure of the document.
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.
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"}
%link{"rel" => "stylesheet", "href" => "main.css", "type" => "text/css"}
%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:
Implementations
The official implementation of Haml has been built for RubyRuby (programming language)
Ruby is a dynamic, reflective, general-purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features. Ruby originated in Japan during the mid-1990s and was first developed and designed by Yukihiro "Matz" Matsumoto...
with plugins for Ruby on Rails
Ruby on Rails
Ruby on Rails, often shortened to Rails or RoR, is an open source web application framework for the Ruby programming language.-History:...
and Merb
Merb
Merb, short for "Mongrel+Erb", is a model–view–controller web framework written in Ruby. Merb adopts an approach that focuses on essential core functionality, leaving most functionality to plugins. Merb was merged into Rails web framework on December 23, 2008 as part of the Ruby on Rails 3.0...
, but the Ruby implementation also functions independently.
There are also implementations in other languages:
- HamlPy (Python)
- LuaHaml (Lua)
- MonoRail NHaml (ASP.NET)
- NHaml (.NET)
- Fammel (PHP)
- HAML-TO-PHP (PHP5)
- pHAML (PHP)
- phamlp (PHP)
- phpHaml (PHP5)
- haml-js (JavaScript)
- Text::Haml (Perl)
- Scalate (Scala)
- JHaml (Java)
Development
Haml was invented in May 2006 by Hampton Catlin. However, Nathan Weizenbaum is responsible for the maturation of the Haml codebase. At this time, Haml is considered a highly stable language.See also
- BBCodeBBCodeBBCode or Bulletin Board Code is a lightweight markup language used to format posts in many message boards. The available tags are usually indicated by square brackets surrounding a keyword, and they are parsed by the message board system before being translated into a markup language that web...
- eRubyERubyeRuby is a templating system that embeds Ruby into a text document. It is often used to embed Ruby code in an HTML document, similar to ASP, JSP and PHP.-Usage:eRuby allows Ruby code to be embedded within a pair of delimiters...
- MarkabyMarkabyMarkaby is a small Ruby library for writing HTML code in pure Ruby. It is an alternative to templating languages such as ERb and HAML which combine Ruby code with some form of markup...
- RubyRuby (programming language)Ruby is a dynamic, reflective, general-purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features. Ruby originated in Japan during the mid-1990s and was first developed and designed by Yukihiro "Matz" Matsumoto...
- Ruby on RailsRuby on RailsRuby on Rails, often shortened to Rails or RoR, is an open source web application framework for the Ruby programming language.-History:...
- Web templateWeb templateA web template is a tool used to separate content from presentation in web design, and for mass-production of web documents. It is a basic component of a web template system.Web templates can be used to set up any type of website...
- YAMLYAMLYAML is a human-readable data serialization format that takes concepts from programming languages such as C, Perl, and Python, and ideas from XML and the data format of electronic mail . YAML was first proposed by Clark Evans in 2001, who designed it together with Ingy döt Net and Oren Ben-Kiki...
- SASS - A similar system for CSS, also designed by Catlin.
External links
- Official Haml Website
- Haml source code repository (Git)
- phpHaml (Haml implementation for PHP 5)
- Introductory Article on Haml on Unspace.ca
- Haml Google Group
- Haml 1.0 announcement on official Ruby on Rails weblog
- Haml 2.0 announcement on Nathan Weizenbaum's blog
- Interview with Haml creator on official Rails podcast
- Interview with Nathan Weizenbaum and Chris Eppstein Haml, Sass and Compass maintainers on The Changelog podcast
- Rails Wiki: Using Haml with Ruby on Rails