Product Code Database
Example Keywords: water filter -netbooks $17-166
barcode-scavenger
   » » Wiki: Haml
Tag Wiki 'Haml'.
Tag

Haml ( HTML Abstraction Markup Language) is a templating system that is designed to avoid writing inline code in a and make the cleaner. Similar to other template systems like , 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 code while developing a .

While parsing code comments, Haml uses the same rules as Ruby 1.9 or later. Haml understands only -compatible encodings, like UTF-8, but not UTF-16, or UTF-32, because these are not compatible with .

Haml can be used at the command line, as a separate Ruby module, or in a Ruby on Rails application.


History
Haml was originally introduced by with its initial release in 2006 and his work was taken up by a few other people. His motive was to make 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 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 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.


"Hello, World!"

Haml as a command-line tool
The following are equivalent as HAML recognises CSS selectors: %p{:class => "sample", :id => "welcome"} Hello, World! %p.sample#welcome Hello, World!

These render to the following code:

Hello, World!


Haml as an add-on for Ruby on Rails
Haml can be integrated into Ruby on Rails as a plugin. Similar to , 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.

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, replacing open-end tag pairs. The following example compares the syntaxes of Haml and (Embedded Ruby), alongside the HTML output.

%div.category
   %div.recipes
       %h1= recipe.name
       %h3= recipe.category
   %div
       %h4= recipe.description
     
   

<%= recipe.name %>

<%= recipe.category %>

<%= recipe.description %>

   

Cookie

Desserts

Made from dough and sugar. Usually circular in shape and has about 400 calories.

Key differences are:

  • Haml doesn't have both opening and closing tags for each element like .
  • syntax looks a lot like and is thereby more HTML-like while Haml is more CSS-like.
  • Haml uses 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
!!! %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 :

 
   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 new Templating Engine out for Ruby on Rails. It's called Haml.


Implementations
The official implementation of Haml has been built for Ruby with plugins for Ruby on Rails and , 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:


See also
  • Ruby
  • Ruby on Rails
  • Sass – a similar system for CSS, also designed by Catlin.
  • Website Meta Language – another template language with similar functionalities
  • – general concept of template to expansion


External links

Page 1 of 1
1
Page 1 of 1
1

Account

Social:
Pages:  ..   .. 
Items:  .. 

Navigation

General: Atom Feed Atom Feed  .. 
Help:  ..   .. 
Category:  ..   .. 
Media:  ..   .. 
Posts:  ..   ..   .. 

Statistics

Page:  .. 
Summary:  .. 
1 Tags
10/10 Page Rank
5 Page Refs
1s Time