3 ms·
Well, the advantages are more evident when you generate HTML dynamically from any source. Typically the data you want to render will already be passed in in the
by loevborg 11y ago
Well, the advantages are more evident when you generate HTML dynamically from any source. Typically the data you want to render will already be passed in in the form of Clojure data structures (sequences). With hiccup syntax, all you need to do is produce yet another clojure data structure. This gives you a very simple task: when I have this vector, how do I produce that vector?
Because you're basically massaging vectors and maps, you have all the standard tools from `clojure.core` at your disposal. As an example (untested):
(def render-posts [posts]
(->> posts
(map (juxt :title :url))
(filter (fn [[_ url]] (not (empty? url)))
(map (fn [[title url]] [:ul.titles [:a {:href url} title]]))
(into [:div.titles]))))
If you're not sure how to get there, you can play around in the REPL with test data, adding one step after another until you have the right structure of vectors. With time, working with these structures becomes second nature.
- escherize 11y agoJust tonight I created a read only dashboard from a seq of business-logicy data maps. Making tables and specialized formatting by applying functions is a breeze with this approach.