Hiccup - HTML Libraryλ︎
Hiccup is a library for generating HTML from Clojure, keeping your code consistent and easier to write and maintain.
Using HTML, a heading would be written as:
Hiccup uses vectors to define HTML tags and maps to represent styles and other attributes. So the same heading in Hiccup would be written as:
Using Hiccupλ︎
Add the hiccup dependency to your project.clj
file
In the REPL, require the hiccup.core library
Or add hiccup to the namespace definition in your Clojure code file.
Writing Hiccupλ︎
Here is a basic example of Hiccup syntax:
The first element of the vector is used as the element name. The second attribute can optionally be a map, in which case it is used to supply the element's attributes. Every other element is considered part of the tag's body.
Hiccup is intelligent enough to render different HTML elements in different ways, in order to accommodate browser quirks:
And provides a CSS-like shortcut for denoting id
and class
attributes:
When writing multiple lines of hiccup markup, wrap them in either a [:div ]
or a (list )
expression.
[:div
[:h1 "My Picture Album"]
[:img {:src seaside.png} "A sunny seaside view"]
[:img {:src pier.png} "A walk along the pier"]]
If the body of the element is a seq, its contents will be expanded out into the element body. This makes working with forms like map
and for
more convenient:
The parent tag will still be rendered in the above example, so
Hint:: Hiccup reference and guidesλ︎