This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-04-29
Channels
- # babashka (168)
- # beginners (60)
- # calva (21)
- # cider (44)
- # clj-kondo (27)
- # cljs-dev (4)
- # cljsrn (2)
- # clojure (157)
- # clojure-australia (27)
- # clojure-bay-area (7)
- # clojure-czech (1)
- # clojure-europe (94)
- # clojure-nl (2)
- # clojure-spec (3)
- # clojure-sweden (11)
- # clojure-uk (35)
- # clojurescript (19)
- # cursive (12)
- # events (1)
- # figwheel-main (1)
- # fulcro (28)
- # honeysql (7)
- # jackdaw (9)
- # kaocha (4)
- # keechma (1)
- # malli (7)
- # midje (1)
- # missionary (1)
- # music (1)
- # off-topic (45)
- # pathom (18)
- # polylith (6)
- # practicalli (4)
- # random (1)
- # reagent (3)
- # reitit (3)
- # shadow-cljs (74)
- # sql (9)
I am looking for some similar example like below (from John's banking app example) but for populating an HTML table. Any suggestions?
;; Abstracting hiccup with functions
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn unordered-list [items]
[:ul
(for [i items]
[:li i])])
;; Many lines of code can now be reduced to a single line
;; [:div
;; (unordered-list ["collection" "of" "list" "items"])]
Simple example for data:
(def player-data [{:name "Oliver", :score 100} {:name "Revilo", :score 50}])
Finally figured out something 😅
(html5 (for [row player-data]
[:tr (map (fn [x] [:td (val x)]) row)]))
;; => "\n<html><tr><td>Oliver</td><td>100</td></tr><tr><td>Revilo</td><td>50</td></tr></html>"
@oliver.heck I think a more typical approach would be to use a let within the for
expression. As for
is a macro, then you can use the :let
shorter form.
This code provides the same output
(html5 (for [row player-data
:let [player (:name row)
score (:score row)]]
[:tr [:td player] [:td score]]))
😀 3
The last part of the for expression uses the local names created by the let to populate the hiccup structure as the code iterates through player-data