Clojure beginner here! I added flash message support to my Hiccup code in a Noir project.
What I'm trying to do is check if the message string for each specific was set or not. If there's no message, then I don't want to display the specific flash element containing that message.
(defpartial success-flash [msg]
[:div.alert.notice.alert-success
[:a.close {:data-dismiss "alert"} "x"]
[:div#flash_notice msg]])
(defpartial error-flash [msg]
[:div.alert.notice.alert-error
[:a.close {:data-dismiss "alert"} "x"]
[:div#flash_notice msg]])
[..]
(defpartial layout [& content]
(html5
[:head
[...]
[:body
(list
[...]
[:div.container
(let [error-msg (session/flash-get :error-message)
error-div (if (nil? error-msg) () (error-flash error-msg))
success-msg (session/flash-get :success-message)
success-div (if (nil? success-msg) () (success-flash success-msg))]
warning-msg (session/flash-get :warning-message)
warning-div (if (nil? warning-msg) () (warning-flash warning-msg))]
(list error-div success-div warning-div content))])]))
Disclaimer: I completely agree that you won't likely ever be in a situation where you'll need more than one of those specific flashes on at once, but indulge me in my attempt at figuring out a better and more functional way of implementing this.
I'm confident that there's a pattern out there for handling similar situations. Basically I check the value of several expressions, do a bunch of stuff with those values, and then act based on the results. You could pull this off with a progressively more and more monstrous (cond), but my solution is at least somewhat cleaner.
Tips?
You could also use
when-let
.I'm not a hiccup expert, but I think this should work. I find it a little clearer on what's going on, although it's slightly more verbose.