HTML
The html
function generates a string of HTML from a tree of vectors. This
allows HTML to be defined without the need for external template files.
(html [:p "Hello World"]) => "<p>Hello World</p>"
The first item of the vector defines the tag name. This can be a string, symbol or keyword.
The second item can optionally be a map. This defines the set of attributes on the tag.
(html [:img {:src "logo.png"}]) => "<img src=\"logo.png\"/>"
Any further items in the vector are considered to be part of the tag's content. This can also include nested vectors.
(html [:p "Hi " [:em "World"]]) => "<p>Hi <em>World</em></p>"
Any sequence in the vector is automatically expanded.
(html [:p (list "AB" "CD" "EF")]) => "<p>ABCDEF</p>"
This can be useful for producting HTML from sequences of data. For example:
(defn unordered-list [items] [:ul (for [item items] [:li items])])
For defining "id" and "class" attributes, the html
function has some syntax
sugar based on CSS selectors.
(html [:p.greet "Hello"]) => "<p class=\"greet\">Hello</p>" (html [:h1#title "About"]) => "<h1 id=\"title\">About</h1>"