Hiccup – A subset of Clojure used for generating HTML
hiccup.space
hiccup.space
Something that is very useful is that hiccup automatically ignores `nil` when putting together contents, so you can use
(when pred [:div "..."])
if you want to show a div conditionally.
To me it looks considerably worse than other nicer-than-HTML syntaxes like Haml and Jade and I'm not sure what the supposed benefits are of writing it in a lispy, paren-y style -- is there really a homoiconicity benefit here as that seems especially unlikely when dealing with already-declarative code that is designed to be dumb.
Aside: I always feel like the homoiconicity argument is completely the wrong trade-off -- sacrificing readable syntax for the ability to write easier macros? What team has ever been happy to find out that one of their team members thought themselves smart enough to start writing macros?
Don't get. I bought the Clojure book and I less-than-half-get-it (as demonstrated by my above stance on homoiconicity) and if someone held a gun to my head and made me write a web-app in ClojureScript I'm pretty sure I'd still use a conventional templating engine for my HTML templates (mustache-style + HTML).
Would love to be persuaded otherwise because I'm thoroughly confused by this everything-as-an-s-expression mentality of lispers and I'm concerned about the amount of dust gathering on my Clojure book.
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.I write apps with React, Rum and Semantic-UI, and the resulting code is really nice to read. You get things like:
[:div.three.wide.required.field [:label "Name"] [:input ...]]
(incidentally, I use Sablono, not Hiccup, but it doesn't matter, the syntax is the same)
The data structure is HTML structure makes it easy to manipulate things. Coupled to a functional language where I can pass functions around in the view (try doing that within JSP), I get a lot of reuse. I couldn't get the same level of reuse if I was using a string templating system.
Great work, btw.
Source is from the ClojureD conference in Berlin, Germany and Published on Feb 23, 2015
Highly recommend watching and it is only 32 minutes in duration.
However, its big drawback is that it's unsafe by default. You have to opt in to escaping everything you send through it.
Though I believe Reagent's Hiccup-like syntax and https://github.com/r0man/sablono (for Om) are safe.
</shameless-plug>
FWIW, Eric Naggum has a good point: the syntactic separation is not good: http://www.schnada.de/grapt/eriknaggum-enamel.html
Having a function who's job is to take a map and turn it into hiccup is extremely powerful, wether you're building a card-like interface, a table, or really anything.
https://github.com/Matt-Esch/virtual-dom
Makes server-side and client side rendering a cinch. Here is an example of it in action in mercury:
https://github.com/Raynos/mercury/blob/master/examples/serve...