Conditionals in Hiccup, can I make this more idiomatic?

488 views Asked by At

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?

2

There are 2 answers

1
kotarak On BEST ANSWER

You could also use when-let.

(defpartial layout
  [& contents]
  (html5
    [:body
     (when-let [msg (session/flash-get :error-message)]
       (error-flash msg))
     (when-let [msg (session/flash-get :warning-message)]
       (warning-flash msg))
     (when-let [msg (session/flash-get :success-message)]
       (success-flash msg))
     contents))

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.

0
Ankur On

The pattern is called mapping value. Below is an example that uses keep function to apply the pattern of mapping values and then filtering them

(use 'clojure.contrib.core)


(def flash-message
[[:error-message error-flash]
 [:success-message success-flash]
 [:warning-message warning-flash]])

(keep (fn [m f] (-?>> m (session/flash-get) (f))) flash-message)