11 HAML
On this page:
11.1 Syntax
haml

11 HAML

 (require koyo/haml) package: koyo-lib

haml is a convenience macro for generating xexprs with less boilerplate. Its syntax is inspired by haml – whence its name – but if you’ve used Clojure’s hiccup or any similar templating language (like jade or pug), then haml will seem familiar to you.

11.1 Syntax

syntax

(haml element ...+)

 
element = (selector attributes element ...)
  | (selector element ...)
  | symbol
  | unless
  | when
  | splice
  | expr
     
selector = :tag-name
  | .class-name
  | .class-name-1.class-name-2.class-name-n#id
  | :tag-name.class-name
  | :tag-name.class-name#id
     
symbol = &name
     
attributes = ((:attribute-name maybe-expr) ...)
     
unless = (unless cond-expr e0 e ...)
     
when = (when cond-expr e0 e ...)
     
splice = ,@e
Produces an x-expression.

Literal numbers and strings are returned unmodified:

> (haml 1)

1

> (haml "hello")

"hello"

Identifiers that start with an & symbol are returned with said symbol stripped off:

> (haml &mdash)

'mdash

Other identifiers are evaluated from the enclosing environment:

> (let ([a-symbol 'mdash])
    (haml a-symbol))

'mdash

Element tags start with a colon:

> (haml
   (:h1 "Hello World"))

'(h1 () "Hello World")

Id and class attributes can be attached to a tag via a shorthand syntax:

> (haml
   (:h1.title#main-title "Hello World"))

'(h1 ((id "main-title") (class "title")) "Hello World")

Tag names are optional if a class name is provided:

> (haml
   (.content
    (:h1.title "Hello World")))

'(div ((class "content")) (h1 ((class "title")) "Hello World"))

Lists of elements can be spliced in using the ,@e syntax:

> (haml
   (.content
    (:ul.items
     ,@(for/list ([it (list "a" "b" "c")])
         (haml (:li it))))))

'(div ((class "content")) (ul ((class "items")) (li () "a") (li () "b") (li () "c")))

All expressions that don’t parse as an element are evaluated in place at runtime:

> (define (say-hi name)
    (format "Hi, ~a!" name))
> (haml
   (:h1 (say-hi "Bogdan")))

'(h1 () "Hi, Bogdan!")

Passing multiple elements to the haml macro produces a list of xexpr?s:

> (haml
   (:li "a")
   (:li "b"))

'((li () "a") (li () "b"))