ArticleslgStudy

computer science

Haml

Haml is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Haml rather than just read about it. In short: 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.

Key takeaways

  • Haml belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Haml to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Haml from memory before moving on to harder problems.

Reference excerpt

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

These render to the following HTML code:

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.

file: app/controllers/messages_controller.rb file: app/views/messages/index.html.haml This renders to:

Haml as a Ruby module Haml is also capable of being used independently as a Ruby library.

Output:

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.

Key differences are:

Haml doesn't have both opening and closing tags for each element like eRuby. eRuby syntax looks a lot like HTML 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

The above Haml would produce this XHTML:

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:

Ruby: hamlit PHP: Fammel, pHAML, phamlp, phpHaml (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: Scalate

See also

BBCode eRuby Markaby Ruby Ruby on Rails YAML Sass – 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 expansion

References

External links Official website Haml tutorial Learn Haml (basic) Haml on GitHub phpHaml, Haml implementation for PHP Haml Google Group Haml 1.0 Archived 2017-05-19 at the Wayback Machine, announcement on the official Ruby on Rails weblog

Worked examples

Example 1 — a first encounter with Haml

Start with the simplest possible case. Write down what Haml claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Haml before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Haml ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Haml

In research
Haml appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Haml in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Haml is common in secondary-school and first-year university syllabi. It links to neighbouring topics Free computer libraries, Lightweight markup languages, Ruby (programming language), so understanding it makes those chapters shorter.
In everyday life
Look for Haml outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Haml” →

How to study Haml in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Haml means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Haml out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Haml in simple terms?

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 pr…

Why does Haml matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Haml?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Haml.

Tags

  • Free computer libraries
  • Lightweight markup languages
  • Ruby (programming language)
  • Software using the MIT license
  • Template engines

Keep exploring