Haml

Haml
Paradigm Template engine
Designed by Nick Walsh
Developer
Stable release
4.0.7 / August 10, 2015 (2015-08-10)[1]
Implementation language Ruby
OS Cross-platform
License
Filename extensions .haml
Website haml.info

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.[3][4]

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.

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.[5] 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.[5] Natalie Weizenbaum worked on making Haml usable in Ruby applications, while the branding and design was done by Nick Walsh.[5] Others who are currently in the maintenance team are Matt Wildig, Akira Matsuda and Tee Parham.[2]

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.[6] 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.[1] The 5th version is unreleased [1] and is compatible with only Ruby 2.0.0 or above and does not support Rails 3.[1] A 'trace'[7] option, which helps users to perform tracing on Haml template, has been added. Also, certain memory and performance enhancements have been made.[1]

Features

Four principles were involved in development of Haml.[5]

User-friendly markup

Markup language is user-friendly if it adheres to following features:

DRY

Markup language should adhere to the Don't repeat yourself (DRY) principle. It should:

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

%p{:class => "sample", :id => "welcome"} Hello, World!

This renders to this HTML code:

<p class="sample" id="welcome">Hello, World!</p>

To run Haml code, Haml gem must be installed as follows:[8]

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.[8]

This renders to:

<div id="welcome">
    <p>Hello, World!</p>
</div>

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.

welcome = Haml::Engine.new("%p Hello, World!")
welcome.render

Output:

<p>Hello, World!</p>

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

Haml ERB
%div.category
    %div.recipes
        %h1= recipe.name
        %h3= recipe.category
    %div
        %h4= recipe.description
<div class="category">
    <div class="recipes">
        <h1><%= recipe.name %></h1>
        <h3><%= recipe.category %></h3>
    </div>
    <div>
        <h4><%= recipe.description %></h4>
    </div>
</div>

For a sample recipe information, the HTML code rendered by both the above code samples looks like:

<div class="category">
    <div class="recipes">
        <h1>Cookie</h1>
        <h3>Desserts</h3>
    </div>
    <div>
        <h4>Made from dough and sugar. Usually circular in shape and has about 400 calories.</h4>
    </div>
</div>

Key differences are:

Example with embedded Ruby code

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:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang='en' xml:lang='en' xmlns='http://www.w3.org/1999/xhtml'>
  <head>
    <title>BoBlog</title>
    <meta content='text/html; charset=utf-8' http-equiv='Content-Type' />
    <link href="/stylesheets/main.css" media="screen" rel="Stylesheet" type="text/css" />
  </head>
  <body>
    <div id='header'>
      <h1>BoBlog</h1>
      <h2>Bob's Blog</h2>
    </div>
    <div id='content'>
      <div class='entry'>
        <h3 class='title'>Halloween</h3>
        <p class='date'>Tuesday, October 31, 2006</p>
        <p class='body'>
          Happy Halloween, glorious readers! I'm going to a party this evening... I'm very excited.
        </p>
      </div>
      <div class='entry'>
        <h3 class='title'>New Rails Templating Engine</h3>
        <p class='date'>Friday, August 11, 2006</p>
        <p class='body'>
          There's a very cool new Templating Engine out for Ruby on Rails. It's called Haml.
        </p>
      </div>
    </div>
    <div id='footer'>
      <p>
        All content copyright © Bob
      </p>
    </div>
  </body>
</html>

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:

See also

References

  1. 1 2 3 4 5 "Change Log". Retrieved 30 January 2016.
  2. 1 2 "GitHub". Retrieved 30 January 2016.
  3. "Encoding". Retrieved 29 January 2016.
  4. "UTF encodings". Retrieved 7 February 2016.
  5. 1 2 3 4 "History". Retrieved 29 January 2016.
  6. "All Versions". Retrieved 29 January 2016.
  7. "Trace Option in Haml". Retrieved 16 February 2016.
  8. 1 2 "Using Haml". Retrieved 7 February 2016.
This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.