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 tem ...
, 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 sa ...
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 of ...
compatible encodings like
UTF-8 UTF-8 is a variable-width encoding, 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'' ...
but not
UTF-16 UTF-16 (16-bit computing, 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 variab ...
or
UTF-32 UTF-32 (32- bit Unicode Transformation Format) is a fixed-length encoding used to encode Unicode code points that uses exactly 32 bits (four bytes) per code point (but a number of leading bits must be zero as there are far fewer than 232 Unicode ...
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 of ...
. 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 we ...
application.


History

Haml was originally introduced by
Hampton Catlin Hampton Lintorn-Catlin ( né Catlin; born 1982) is an American computer programmer, programming language inventor, gay rights advocate, and author, best known as the creator of the Sass and Haml markup languages. Hampton was a Vice President of ...
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 sa ...
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 Rail or rails may refer to: Rail transport *Rail transport and related matters *Rail (rail transport) or railway lines, the running surface of a railway Arts and media Film * ''Rails'' (film), a 1929 Italian film by Mario Camerini * ''Rail'' ( ...
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 Trace may refer to: Arts and entertainment Music * ''Trace'' (Son Volt album), 1995 * ''Trace'' (Died Pretty album), 1993 * Trace (band), a Dutch progressive rock band * ''The Trace'' (album) Other uses in arts and entertainment * ''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) principle. It should: * Avoid unnecessary repetitions * Focus on clean code


Well-indented

Markup language with good
indentation __FORCETOC__ In the written form of many languages, an indentation or indent is an empty space at the beginning of a line to signal the start of a new paragraph. Many computer languages have adopted this technique to designate "paragraphs" or o ...
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 Cascading Style Sheets (CSS) is a style sheet language used for describing the presentation of a document written in a markup language such as HTML or XML (including XML dialects such as SVG, MathML or XHTML). CSS is a cornerstone techno ...
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

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 we ...

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 sa ...
, 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 tem ...
, 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 Rail or rails may refer to: Rail transport *Rail transport and related matters *Rail (rail transport) or railway lines, the running surface of a railway Arts and media Film * ''Rails'' (film), a 1929 Italian film by Mario Camerini * ''Rail'' ( ...
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 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 tem ...
(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 tem ...
* 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 Cascading Style Sheets (CSS) is a style sheet language used for describing the presentation of a document written in a markup language such as HTML or XML (including XML dialects such as SVG, MathML or XHTML). CSS is a cornerstone techno ...
-like. * Haml uses
indentation __FORCETOC__ In the written form of many languages, an indentation or indent is an empty space at the beginning of a line to signal the start of a new paragraph. Many computer languages have adopted this technique to designate "paragraphs" or o ...
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 sa ...
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 we ...
and
Merb Merb is a discontinued model–view–controller web framework in Ruby, notable as a precursor to Rails 3. It brought increased focus on speed and modularity to Rails 3. The name Merb is a contraction of "Mongrel" and "Erb". Precursor to and m ...
, 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_.html" ;"title="/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 tem ...
*
Markaby Markaby 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. It was developed by the computer programmer " why the lucky ...
*
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 sa ...
*
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 we ...
*
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 Exte ...
* Sass – a similar system for
CSS Cascading Style Sheets (CSS) is a style sheet language used for describing the presentation of a document written in a markup language such as HTML or XML (including XML dialects such as SVG, MathML or XHTML). CSS is a cornerstone techno ...
, also designed by Catlin. *
Website Meta Language Website Meta Language (WML) and its associated command wmk are together a free and extensible web designer's off-line HTML generation toolkit for Unix, distributed under the GNU General Public License (GPL v2). It works as an off-line content ...
– another template language with similar functionalities *
Web template A web template system in web publishing lets web designers and developers 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 dynamic el ...
– 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 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 implementation is now produced by The PHP Group. ...

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