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 easy and clean. 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.
Video Haml
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. In April 2012, the maintenance of Haml was taken over by Norman Clarke. Natalie Weizenbaum 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 and Tee Parham.
Version history
The latest version of Haml as a gem is 4.0.7 and 4.1.0 series is out with its alpha and beta versions. 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. The 5th version is unreleased and is compatible with only Ruby 2.0.0 or above and does not support Rails 3. A 'trace' option, which helps users to perform tracing on Haml template, has been added. Also, certain memory and performance enhancements have been made.
Maps Haml
Features
Four principles were involved in development of Haml.
User-friendly markup
Markup language is user-friendly if it adheres to following features:
- Easy to understand the language
- Easy of use (Implementation)
DRY
Markup language should adhere to the Don't repeat yourself (DRY) principle. It should:
- Avoid unnecessary repetitions
- Focus on clean code
Well-indented
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" example
A simple Hello World implementation in Haml would be:
Haml as a command-line tool
This renders to this HTML code:
To run Haml code, Haml
gem must be installed as follows:
gem install haml
Haml code that is saved to a file named as Hello.haml
, can be run as follows:
haml Hello.haml
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 (declared within same file in Ruby code). This example uses a sample Ruby controller file.
- file: app/controllers/messages_controller.rb
- file: app/views/messages/index.html.haml
This renders to:
Haml as a Ruby module
To use Haml independent of Rails and ActionView, install haml
gem, include it in Gemfile
and simply import [Usage: require 'haml']
it in Ruby script or invoke Ruby interpreter with -rubygems
flag.
Output:
Haml::Engine is a Haml class.
Basic example
Haml uses whitespace indentation (two spaces) to represent nesting of tags and identifying scope of a given tag. This acts as a replacement for the open-end tag pairs, making it DRY and clear to look at (easy to read). The following example demonstrates the differences between Haml and eRuby (Embedded Ruby).
For a sample recipe information, the HTML code rendered by both the above code samples looks like:
Key differences are:
- Haml doesn't have both start and end for each element like eRuby
- eRuby syntax looks a lot like HTML and is thereby more HTML-like while Haml is more CSS-like.
- Haml uses indentation to nest tag elements whereas eRuby uses the same HTML representation
- In Haml properties like
class
,id
can be represented by.
,#
respectively instead of regularclass
andid
keywords. Haml also uses%
to indicate a HTML element instead of<>
as in eRuby.
Example with embedded Ruby code
Note: This is a simple preview example and may not reflect the current version of the language.
The above Haml would produce this XHTML:
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:
- hamlit (Ruby)
- HamlPy (Python)
- LuaHaml (Lua)
- MonoRail NHaml (ASP.NET)
- NHaml (.NET)
- Fammel (PHP)
- HAML-TO-PHP (PHP5)
- pHAML (PHP)
- phamlp (PHP)
- phpHaml (PHP5)
- Multi target HAML (PHP5.3)
- haml-js (JavaScript)
- Text::Haml (Perl)
- Scalate (Scala)
- JHaml (Java)
- Hart (Dart)
- cl-haml (Common Lisp)
See also
- BBCode
- eRuby
- Markaby
- Ruby
- 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 - general concept of template to HTML expansion
References
External links
- Official website
- Haml tutorial
- Learn Haml (basic)
- Haml on GitHub
- phpHaml, Haml implementation for PHP
- Haml Google Group
- Haml 1.0, announcement on the official Ruby on Rails weblog
Source of article : Wikipedia