Search code examples
clojureclojurescript

How to return hiccup as a list/vector


I am passing a vector of hiccup to a funtion that just wraps it in more hiccup, but it does not return it as I would expect.

Here's an example of what I mean:

(defn wrap-fn 
  [input]
  [div.content-box
    [input]])

(defn main-fn 
  [vector-of-hiccup]
  (foreach [hiccup from vector-of-hiccup]
    (wrap-fn hiccup-from-list)))

How do I implement the made up foreach loop above?

I've tried to use 'apply' to apply the wrap-fn to each of the vector params but it only returns the first element wrapped. I've tried to creating all sorts of loops and I have similar levels of success.

I'm sure there is a way to do this, please help me find one that works.


Solution

  • You need something like this:

    (defn wrap-fn
      [input]
      [:div.content-box
       [input]])  ; <= you may not want to wrap `input` in a vector.
    
    (defn main-fn
      [vector-of-hiccup]
      (vec
        (for [item vector-of-hiccup]
          (wrap-fn item))))
    

    Note the : in :div. Hiccup vectors always start with a keyword. Also, since for returns a lazy sequence, you should convert it into a vector with vec.

    Also, depending on your situation, you may want to have input instead of [input] under the :div.content-box.

    See the Documentation section of the clj-template project for valuable learning information.