Haml

Last updated
Haml
Paradigm Template engine
Designed by Hampton Catlin
Developers Natalie Weizenbaum (past), Norman Clarke, Matt Wildig, Akira Matsuda, Tee Parham [1]
Stable release
6.3.0 [2]   OOjs UI icon edit-ltr-progressive.svg / 10 December 2023;5 months ago (10 December 2023)
Implementation language Ruby
OS Cross-platform
License MIT License and Unspace Interactive [1]
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 cleaner. Similar to other 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 a .haml extension. These files are similar to .erb or .eRuby files, which also help embed Ruby code while developing a web application.

Contents

While parsing code 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 at the command line, as a separate Ruby module, or in a Ruby on Rails application.

History

Haml was originally introduced by Hampton Catlin with its initial release in 2006 and his work was taken up 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 have been released. Until 2012, Natalie Weizenbaum was the primary maintainer of Haml, followed by Norman Clarke until 2015. [5] Natalie worked on making Haml usable in Ruby applications, while the branding and design were done by Nick Walsh. [5]

Version 2.2.0 was released in July 2009 with support for Ruby 1.9 and Rails 2.0 or above. [6] 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. [6] Version 5.0.0 was released in April 2017. It supports Ruby 2.0.0 or above and drops compatibility with Rails 3. [6] A 'trace' [7] 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 HTML code:

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

Haml as an add-on for Ruby on Rails

Haml can be integrated into Ruby on Rails as a plugin. 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]

  • file: app/controllers/messages_controller.rb
    classMessagesController<ApplicationControllerdefindex@message="Hello, World!"endend
  • file: app/views/messages/index.html.haml
    #welcome%p=@message

This renders to:

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

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:

<p>Hello, World!</p>

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 eRuby (Embedded Ruby), alongside the HTML output.

HamlERBHTML
%div.category%div.recipes%h1=recipe.name%h3=recipe.category%div%h4=recipe.description
<divclass="category"><divclass="recipes"><h1><%=recipe.name%></h1><h3><%=recipe.category%></h3></div><div><h4><%=recipe.description%></h4></div></div>
<divclass="category"><divclass="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

!!!%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.eachdo|entry|.entry%h3.title=entry.title%p.date=entry.posted.strftime("%A, %B %d, %Y")%p.body=entry.body#footer%pAll 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"><htmllang='en'xml:lang='en'xmlns='http://www.w3.org/1999/xhtml'><head><title>BoBlog</title><metacontent='text/html; charset=utf-8'http-equiv='Content-Type'/><linkhref="/stylesheets/main.css"media="screen"rel="Stylesheet"type="text/css"/></head><body><divid='header'><h1>BoBlog</h1><h2>Bob's Blog</h2></div><divid='content'><divclass='entry'><h3class='title'>Halloween</h3><pclass='date'>Tuesday, October 31, 2006</p><pclass='body'>           Happy Halloween, glorious readers! I'm going to a party this evening... I'm very excited.         </p></div><divclass='entry'><h3class='title'>New Rails Templating Engine</h3><pclass='date'>Friday, August 11, 2006</p><pclass='body'>           There's a new Templating Engine out for Ruby on Rails. It's called Haml.         </p></div></div><divid='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

Related Research Articles

<span class="mw-page-title-main">HTML</span> HyperText Markup Language

HyperText Markup Language (HTML) is the standard markup language for documents designed to be displayed in a web browser. It defines the content and structure of web content. It is often assisted by technologies such as Cascading Style Sheets (CSS) and scripting languages such as JavaScript.

DocBook is a semantic markup language for technical documentation. It was originally intended for writing technical documents related to computer hardware and software, but it can be used for any other sort of documentation.

An HTML element is a type of HTML document component, one of several types of HTML nodes. The first used version of HTML was written by Tim Berners-Lee in 1993 and there have since been many versions of HTML. The current de facto standard is governed by the industry group WHATWG and is known as the HTML Living Standard.

<span class="mw-page-title-main">Ruby on Rails</span> Server-side open source web application framework

Ruby on 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 pages. It encourages and facilitates the use of web standards such as JSON or XML for data transfer and HTML, CSS and JavaScript for user interfacing. In addition to MVC, Rails emphasizes the use of other well-known software engineering patterns and paradigms, including convention over configuration (CoC), don't repeat yourself (DRY), and the active record pattern.

Apache Wicket, commonly referred to as Wicket, is a component-based web application framework for the Java programming language conceptually similar to JavaServer Faces and Tapestry. It was originally written by Jonathan Locke in April 2004. Version 1.0 was released in June 2005. It graduated into an Apache top-level project in June 2007.

In computing, quirks mode is an approach used by web browsers to maintain backward compatibility with web pages designed for old web browsers, instead of strictly complying with web standards in standards mode. This behavior has since been codified, so what was previously standards mode is now referred to as simply no quirks mode.

In HTML, the standard markup language for documents designed to be displayed in a web browser, <div> and <span> tags are elements used to define parts of a document, so that they are identifiable when a unique classification is necessary. Where other HTML elements such as <p> (paragraph), <em> (emphasis), and so on, accurately represent the semantics of the content, the additional use of <span> and <div> tags leads to better accessibility for readers and easier maintainability for authors. Where no existing HTML element is applicable, <span> and <div> can valuably represent parts of a document so that HTML attributes such as class, id, lang, or dir can be applied.

Embedded Ruby 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 templating system of eRuby combines Ruby code and plain text to provide flow control and variable substitution, thus making the combined code easier to maintain.

RDFa or Resource Description Framework in Attributes is a W3C Recommendation that adds a set of attribute-level extensions to HTML, XHTML and various XML-based document types for embedding rich metadata within Web documents. The Resource Description Framework (RDF) data-model mapping enables its use for embedding RDF subject-predicate-object expressions within XHTML documents. It also enables the extraction of RDF model triples by compliant user agents.

<span class="mw-page-title-main">Web template system</span> System in web publishing

A web template system in web publishing allows web designers and developers to 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 elements based on web request parameters. Web templates support static content, providing basic structure and appearance. Developers can implement templates from content management systems, web application frameworks, and HTML editors.

Extensible HyperText Markup Language (XHTML) is part of the family of XML markup languages which mirrors or extends versions of the widely used HyperText Markup Language (HTML), the language in which Web pages are formulated.

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 stiff" and its name comes from Markup as Ruby.

In computing, Facelets is an open-source Web template system under the Apache license and the default view handler technology for Jakarta Faces. The language requires valid input XML documents to work. Facelets supports all of the JSF UI components and focuses completely on building the JSF component tree, reflecting the view for a JSF application.

<span class="mw-page-title-main">EPUB</span> E-book format

EPUB is an e-book file format that uses the ".epub" file extension. The term is short for electronic publication and is sometimes stylized as ePub. EPUB is supported by many e-readers, and compatible software is available for most smartphones, tablets, and computers. EPUB is a technical standard published by the International Digital Publishing Forum (IDPF). It became an official standard of the IDPF in September 2007, superseding the older Open eBook (OEB) standard.

Apache Click is a page and component oriented web application framework for the Java language and is built on top of the Java Servlet API.

XHTML+RDFa is an extended version of the XHTML markup language for supporting RDF through a collection of attributes and processing rules in the form of well-formed XML documents. XHTML+RDFa is one of the techniques used to develop Semantic Web content by embedding rich semantic markup. Version 1.1 of the language is a superset of XHTML 1.1, integrating the attributes according to RDFa Core 1.1. In other words, it is an RDFa support through XHTML Modularization.

Foundation is a free responsive front-end framework, providing a responsive grid and HTML and CSS UI components, templates, and code snippets, including typography, forms, buttons, navigation and other interface elements, as well as optional functionality provided by JavaScript extensions. Foundation is an open source project, and was formerly maintained by ZURB. Since 2019, Foundation has been maintained by volunteers.

<span class="mw-page-title-main">Yesod (web framework)</span>

Yesod is a web framework based on the programming language Haskell for productive development of type-safe, representational state transfer (REST) model based, high performance web applications, developed by Michael Snoyman, et al. It is free and open-source software released under an MIT License.

<span class="mw-page-title-main">Journal Article Tag Suite</span>

The Journal Article Tag Suite (JATS) is an XML format used to describe scientific literature published online. It is a technical standard developed by the National Information Standards Organization (NISO) and approved by the American National Standards Institute with the code Z39.96-2012.

Framework7 is an open source and free framework to develop mobile, desktop or web apps; Framework7 can also be used as a prototyping tool.

References

  1. 1 2 "GitHub". GitHub . Retrieved 28 April 2018.
  2. "Release 6.3.0". 10 December 2023. Retrieved 19 December 2023.
  3. "Encoding" . Retrieved 29 January 2016.
  4. "UTF encodings". GitHub . Retrieved 7 February 2016.
  5. 1 2 3 "About" . Retrieved 28 April 2018.
  6. 1 2 3 "Changelog". GitHub . Retrieved 28 April 2018.
  7. "Trace Option in Haml" . Retrieved 16 February 2016.
  8. "Using Haml" . Retrieved 7 February 2016.