Imagine I want to write a Clojure function that returns a Hiccup structure equivalent to <h3>Hello</h3>.
How can I do it?
I tried
(defn render-location-details
[cur-location]
(let []
(list :h3 "Hello")
)
)
and
(defn render-location-details
[cur-location]
(let []
[:h3 "Hello"]
)
)
but in both cases got the error message (:h3 "Location X") is not a valid element name..
Update 1: I am calling the above function from this one:
(defn generate-location-details-report
[all-locations]
(let
[
hiccup-title [:h2 "Locations"]
hiccup-body (into []
(map
render-location-details
all-locations)
)
]
(str
(html hiccup-title)
hiccup-body
)
)
)
There is a collection all-locations. For each element of it, I want to create a section in HTML (with the h3) header (hiccup-body), prepend the title (hiccup-title) and convert all of this to HTML.
The hiccup
htmlfunction will take a sequence of tags and render them as a string.The first
render-location-detailsdoesn't work because a list isn't a vector, and so Hiccup won't render it as a tag.The second
render-location-detailsis OK and does what you want. The empty(let []binding is unnecessary. However Hiccup is then confused by puttinghiccup-body (into []- it is trying to understand your vector of location tags as a tag, because as far as Hiccup is concerned vector = tag.