This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2024-02-06
Channels
- # aleph (43)
- # announcements (11)
- # babashka (35)
- # beginners (70)
- # calva (4)
- # cider (8)
- # clerk (15)
- # clojure (192)
- # clojure-dev (7)
- # clojure-europe (44)
- # clojure-nl (2)
- # clojure-norway (65)
- # clojure-uk (4)
- # code-reviews (4)
- # conjure (1)
- # cursive (41)
- # data-science (1)
- # datomic (8)
- # emacs (7)
- # fulcro (13)
- # humbleui (17)
- # hyperfiddle (53)
- # kaocha (4)
- # malli (7)
- # missionary (17)
- # music (1)
- # obb (1)
- # off-topic (8)
- # polylith (1)
- # portal (3)
- # releases (11)
- # shadow-cljs (36)
- # squint (4)
- # tools-deps (4)
Is this a reasonable idea?
;; Defines a hiccup generating function. Adds :is-hiccup = true to response map.
(defmacro defhiccup [name body]
(let [request (gensym)]
`(defn ~name [~request]
(-> ~request
(assoc :is-hiccup true)
(assoc :body ~body)))))
;; If response map has :is-hiccup = true, convert body hiccup into html string.
(defn wrap-hiccup-body->html [handler]
(fn [request]
(let [response (handler request)
body (:body response)
response (if (:is-hiccup response)
(assoc response :body (str (hiccup/html body)))
response)]
response)))
;; For reitit
(def hiccup->html-middleware
{:name ::hiccup->html
:description "Convert hiccup responses to html"
:wrap wrap-hiccup-body->html})
(comment
(defhiccup hi-fragment
[:div "Hi"])
(hi-fragment {}) ;;=> {:is-hiccup true, :body [:div "Hi"]}
,,)