Syntax reference

Tags, nesting, and whitespace

Lines starting with % indicate a tag, possibly with content. The content can either be in-line or in an indented block following.

Examples:

julia> haml"%p" |> println
<p></p>

julia> haml"%p Hello, world!" |> println
<p>Hello, world!</p>

julia> haml"""
       %div
         %p First paragraph
         %p Second paragraph
       """ |> println
<div>
  <p>First paragraph</p>
  <p>Second paragraph</p>
</div>

Just % on its own is equivalent to %div:

julia> haml"""
       %
         %p First paragraph
         %p Second paragraph
       """ |> println
<div>
  <p>First paragraph</p>
  <p>Second paragraph</p>
</div>

Add / if the tag should self-close:

julia> haml"%br/" |> println
<br />

Add < to output an indented block in-line:

julia> haml"""
       %p
         Hello, world
       """ |> println
<p>
  Hello, world
</p>


julia> haml"""
       %p<
         Hello, world
       """ |> println
<p>Hello, world</p>

If you want to output a literal % at the start of a line, escape it with \:

julia> haml"""
       %div
         %p this paragraph was obtained using the following code.
         %pre<
           \%p this paragraph was obtained using the following code.
       """ |> println
<div>
  <p>this paragraph was obtained using the following code.</p>
  <pre>%p this paragraph was obtained using the following code.</pre>
</div>

Attributes

id and class

You can add an id attribute by using the # modifier:

julia> haml"%div#navigation" |> println
<div id='navigation'></div>

Similarly, classes can be added using the . modifier:

julia> haml"%span.foo.bar" |> println
<span class='foo bar'></span>

In both cases, omitting the tag name creates a div:

julia> haml"#navigation.foo.bar" |> println
<div id='navigation' class='foo bar'></div>

julia> haml".foo.bar" |> println
<div class='foo bar'></div>

Named tuple syntax

Other attributes can be added using named tuple syntax:

julia> haml"""%a(href="/", title="click me") Click here!""" |> println
<a href='/' title='click me'>Click here!</a>

Any underscores in the key are replaced by dashes. If the desired attribute is not a valid Julia symbol, it can be encoded using Symbol(...) =>:

julia> haml"""%(foo_bar="foo-bar")""" |> println
<div foo-bar='foo-bar'></div>

julia> haml"""
       %html(xmlns = "http://www.w3.org/1999/xhtml", Symbol("xml:lang") => "en", lang="en")
       """ |> println
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'></html>

Collation and booleans

If the value of an attribute is a boolean, its value is either attribute='attribute' (for true) or it is absent (for false):

julia> haml"%input(selected=true)" |> println
<input selected='selected'></input>

julia> haml"%input(selected=false)" |> println
<input></input>

If the value of an attribute is a key/value structure, the attributes are flattened by joining the keys with -:

julia> haml"""
       %a(href="/posts", data=(author_id=123, category=7)) Posts By Author
       """ |> println
<a href='/posts' data-author-id='123' data-category='7'>Posts By Author</a>

When the value of class is a vector, its elements are joined by a space. When the value of id is a vector, its elements are joined by -.

julia> haml"""
       - items = ["foo", "bar"]
       %(id=items)
       %(class=items)
       """ |> println
<div id='foo-bar'></div>
<div class='foo bar'></div>

Julia code

In-line values

A line starting with = introduces a Julia expression whose value should be inserted.

julia> haml"""
       %p How much is 2 + 2?
       %p<
         It is
         = 2 + 2
       """ |> println
<p>How much is 2 + 2?</p>
<p>It is
4</p>

The = sign can also immediately follow a tag on the same line:

julia> haml"""
       %p How much is 2 + 2?
       %p= "It is $(2 + 2)"
       """ |> println
<p>How much is 2 + 2?</p>
<p>It is 4</p>

The expression can flow over several lines as long as the last non-comment character is a ,:

julia> haml"""
       %p= join(["butter", # popular foods
                 "cheese",
                 "eggs"], ", ", ", and ")
       """ |> println
<p>butter, cheese, and eggs</p>
Note

This does not necessarily agree with Julia's parsing rules or its understanding of an incomplete expression. This is deliberate because HAML is more sensitive to indentation than Julia is.

Code blocks

A line starting with - introduces code that should run but not display any value.

julia> haml"""
       - answer = 42
       %dl
         %dt The answer to life, the universe, and everything
         %dd= answer
       """ |> println
<dl>
  <dt>The answer to life, the universe, and everything</dt>
  <dd>42</dd>
</dl>

A particular case is for, while or do syntax. These have their usual effect on the indented HAML block that follows:

julia> haml"""
       %ul
         - for i in 1:3
           %li= i
       """ |> println
<ul>
  <li>1</li>
  <li>2</li>
  <li>3</li>
</ul>


julia> haml"""
       %ul
         - vals = collect(1:3)
         - while !isempty(vals)
           %li= popfirst!(vals)
       """ |> println
<ul>
  <li>1</li>
  <li>2</li>
  <li>3</li>
</ul>


julia> haml"""
       %dl
         - vals = Dict(:foo => 42, :bar => 43)
         - foreach(pairs(vals)) do (key, val)
           %dt= key
           %dd= val
       """ |> println
<dl>
  <dt>bar</dt>
  <dd>43</dd>
  <dt>foo</dt>
  <dd>42</dd>
</dl>

Interpolations

The character $ interpolates a Julia value into the template:

julia> haml"""
       - quality = "scrumptious"
       %p This is $quality cake!
       """ |> println
<p>This is scrumptious cake!</p>


julia> haml"""
       - quality = "scrumptious"
       %p= "This is $quality cake!"
       """ |> println
<p>This is scrumptious cake&#33;</p>

Use \ to escape it:

julia> haml"""
       - quality = "scrumptious"
       %p This is $quality cake!
       %p This is \$quality cake!
       """ |> println
<p>This is scrumptious cake!</p>
<p>This is $quality cake!</p>

Comments

The / character introduces a HTML comment: its content is part of the output but enclosed between <!-- and -->.

julia> haml"""
       %peanutbutterjelly
         / This is the peanutbutterjelly element
         I like sandwiches!
       """ |> println
<peanutbutterjelly>
  <!-- This is the peanutbutterjelly element -->
  I like sandwiches!
</peanutbutterjelly>


julia> haml"""
       /
         %p This doesn't render...
         %div
           %h1 Because it's commented out!
       """ |> println
<!--
  <p>This doesn't render...</p>
  <div>
    <h1>Because it's commented out!</h1>
  </div>
-->

The combination -# introduces a HAML comment: it produces no output and performs no action.

julia> haml"""
       %p foo
       -# This is a comment
       %p bar
       """ |> println
<p>foo</p>
<p>bar</p>

Document type

The characters !!! introduce a document type specification. At the moment only !!! 5 (HTML 5 standard) is supported:

julia> haml"!!! 5"
"<!DOCTYPE html>"