HOME

TheInfoList



OR:

Haml (
HTML The HyperText Markup Language or HTML is the standard markup language for documents designed to be displayed in a web browser. It can be assisted by technologies such as Cascading Style Sheets (CSS) and scripting languages such as JavaScri ...
Abstraction Markup Language) 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 template systems like
eRuby Embedded Ruby (also shortened as ERB) 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 and JSP, and PHP and other server-side scripting languages. The templ ...
, 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 to embed
Ruby A 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 sapp ...
code while developing a
web application A web application (or web app) is application software that is accessed using a web browser. Web applications are delivered on the World Wide Web to users with an active network connection. History In earlier computing models like client-serve ...
. While parsing coding comments, Haml uses the same rules as Ruby 1.9 or later. Haml understands only
ASCII ASCII ( ), abbreviated from American Standard Code for Information Interchange, is a character encoding standard for electronic communication. ASCII codes represent text in computers, telecommunications equipment, and other devices. Because ...
compatible encodings like
UTF-8 UTF-8 is a variable-length character encoding used for electronic communication. Defined by the Unicode Standard, the name is derived from ''Unicode'' (or ''Universal Coded Character Set'') ''Transformation Format 8-bit''. UTF-8 is capable of ...
but not
UTF-16 UTF-16 (16-bit Unicode Transformation Format) is a character encoding capable of encoding all 1,112,064 valid code points of Unicode (in fact this number of code points is dictated by the design of UTF-16). The encoding is variable-length, as cod ...
or UTF-32 because these are not compatible with
ASCII ASCII ( ), abbreviated from American Standard Code for Information Interchange, is a character encoding standard for electronic communication. ASCII codes represent text in computers, telecommunications equipment, and other devices. Because ...
. Haml can be used in
command line A command-line interpreter or command-line processor uses a command-line interface (CLI) to receive commands from a user in the form of lines of text. This provides a means of setting parameters for the environment, invoking executables and pro ...
, as a separate Ruby module, or in a
Ruby on Rails Ruby on Rails (simplified as Rails) is a server-side web application framework written in Ruby under the MIT License. Rails is a model–view–controller (MVC) framework, providing default structures for a database, a web service, and web ...
application.


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 The HyperText Markup Language or HTML is the standard markup language for documents designed to be displayed in a web browser. It can be assisted by technologies such as Cascading Style Sheets (CSS) and scripting languages such as JavaScri ...
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 A 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 sapp ...
applications, while the branding and design was done by Nick Walsh.


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

Markup language Markup language refers to a text-encoding system consisting of a set of symbols inserted in a text document to control its structure, formatting, or the relationship between its parts. Markup is often used to control the display of the document ...
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 Dry or dryness most often refers to: * Lack of rainfall, which may refer to ** Arid regions ** Drought * Dry or dry area, relating to legal prohibition of selling, serving, or imbibing alcoholic beverages * Dry humor, deadpan * Dryness (medica ...
) 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!"


Haml as a

command-line tool A command-line interpreter or command-line processor uses a command-line interface (CLI) to receive commands from a user in the form of lines of text. This provides a means of setting parameters for the environment, invoking executables and pro ...

The following are equivalent as HAML recognises CSS selectors: %p Hello, World! %p.sample#welcome Hello, World! These render to the following
HTML The HyperText Markup Language or HTML is the standard markup language for documents designed to be displayed in a web browser. It can be assisted by technologies such as Cascading Style Sheets (CSS) and scripting languages such as JavaScri ...
code:

Hello, World!


Haml as an add-on for

Ruby on Rails Ruby on Rails (simplified as Rails) is a server-side web application framework written in Ruby under the MIT License. Rails is a model–view–controller (MVC) framework, providing default structures for a database, a web service, and web ...

To use Haml with
Ruby A 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 sapp ...
, the Ruby Gemfile should include this line: gem 'haml' Similar to
eRuby Embedded Ruby (also shortened as ERB) 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 and JSP, and PHP and other server-side scripting languages. The templ ...
, 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

To use Haml independent of Rails an
ActionView
install haml
gem A gemstone (also called a fine gem, jewel, precious stone, or semiprecious stone) is a piece of mineral crystal which, in cut and polished form, is used to make jewelry or other adornments. However, certain rocks (such as lapis lazuli, opal, a ...
, include it in Gemfile and simply import sage: require 'haml'/code> it in Ruby script or invoke Ruby interpreter with -rubygems flag. welcome = Haml::Engine.new("%p Hello, World!") welcome.render Output:

Hello, World!


Haml::Engine
is a Haml class.


Basic example

Haml uses whitespace indentation (two spaces) for tag nesting and scope. This acts as a replacement for the open-end tag pairs, making it
DRY Dry or dryness most often refers to: * Lack of rainfall, which may refer to ** Arid regions ** Drought * Dry or dry area, relating to legal prohibition of selling, serving, or imbibing alcoholic beverages * Dry humor, deadpan * Dryness (medica ...
and cleaner. The following example compares the syntaxes of Haml and
eRuby Embedded Ruby (also shortened as ERB) 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 and JSP, and PHP and other server-side scripting languages. The templ ...
(Embedded Ruby), alongside the HTML output. Key differences are: * Haml doesn't have both start and end for each element like
eRuby Embedded Ruby (also shortened as ERB) 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 and JSP, and PHP and other server-side scripting languages. The templ ...
* eRuby syntax looks a lot like
HTML The HyperText Markup Language or HTML is the standard markup language for documents designed to be displayed in a web browser. It can be assisted by technologies such as Cascading Style Sheets (CSS) and scripting languages such as JavaScri ...
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 regular class and id 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. !!! %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("%A, %B %d, %Y") %p.body= entry.body #footer %p All content copyright © Bob The above Haml would produce this
XHTML Extensible HyperText Markup Language (XHTML) is part of the family of XML markup languages. It mirrors or extends versions of the widely used HyperText Markup Language (HTML), the language in which Web pages are formulated. While HTML, prior ...
: 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 A 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 sapp ...
with plugins for
Ruby on Rails Ruby on Rails (simplified as Rails) is a server-side web application framework written in Ruby under the MIT License. Rails is a model–view–controller (MVC) framework, providing default structures for a database, a web service, and web ...
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: * Ruby
hamlit
* PHP
FammelpHAMLphamlpphpHaml
(PHP 5)
HAML-TO-PHP
(PHP 5)
Multi target HAML
(PHP 5.3) * Javascript
haml-js
* Python
HamlPy
* Common Lisp
cl-haml
* Dart
Hart
* Java
JHaml
* Lua
LuaHaml
* .NET
NHaml
** ASP.NET
MonoRail NHaml
* Perl
Text::Haml
* Scala


See also

*
BBCode BBCode ("Bulletin Board Code") is a lightweight markup language used to format messages in much Internet forum software, first introduced in 1998. The available "tags" of BBCode are usually indicated by square brackets ( and ">/code> and /code> ...
*
eRuby Embedded Ruby (also shortened as ERB) 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 and JSP, and PHP and other server-side scripting languages. The templ ...
* Markaby *
Ruby A 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 sapp ...
*
Ruby on Rails Ruby on Rails (simplified as Rails) is a server-side web application framework written in Ruby under the MIT License. Rails is a model–view–controller (MVC) framework, providing default structures for a database, a web service, and web ...
*
YAML YAML ( and ) (''see '') is a human-readable data-serialization language. It is commonly used for configuration files and in applications where data is being stored or transmitted. YAML targets many of the same communications applications as Ext ...
*
Sass Sass, Saß or SASS may refer to: SASS * M110 Semi-Automatic Sniper System (or M110 SASS) * Safe Amplification Site Society, a non-profit organization that promotes music and the arts within Vancouver, British Columbia, Canada * Shanghai Academ ...
– 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 The HyperText Markup Language or HTML is the standard markup language for documents designed to be displayed in a web browser. It can be assisted by technologies such as Cascading Style Sheets (CSS) and scripting languages such as JavaScri ...
expansion


References


External links

* *
Haml tutorial

Learn Haml (basic)
* {{GitHub, haml/haml, Haml
phpHaml
Haml implementation for
PHP PHP is a General-purpose programming language, general-purpose scripting language geared toward web development. It was originally created by Danish-Canadian programmer Rasmus Lerdorf in 1993 and released in 1995. The PHP reference implementati ...

Haml Google Group

Haml 1.0
announcement on the official Ruby on Rails weblog Ruby (programming language) Template engines Free computer libraries Software using the MIT license Lightweight markup languages