3 ms·
For my money, hiccup (and thus reagent) get the syntax for generating HTML trees right. It's one of those things that I always thought didn't matter, until I tr
by loevborg 11y ago
For my money, hiccup (and thus reagent) get the syntax for generating HTML trees right. It's one of those things that I always thought didn't matter, until I tried it.
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.
- catshirt 11y agoright and the coolest part is that react (or reagent/om?) will handle that in the diff
- nilliams 11y agoCould you explain why the trade-off of 'just-html' vs the overhead of pre-processing so you can use this not-quite-html-with-weird-parens syntax is worth it? 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.
- loevborg 11y agoWell, 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.