Hi Stefan,
The first thing you might want to do is to make use of the helper
functions included with Hiccup. For example:
(use '[hiccup core page form element])
(defn view-layout [& content]
(html5 [:body content]))
(def all-categories
["Cat1" "Cat2" "Cat3"])
(defn view-input []
(view-layout
[:h2 "Find"]
(form-to [:post "/"]
(for [category all-categories]
(checkbox "categories[]" false category))
[:br]
(text-field {:placeholder "insert manga name"} :a) [:br]
(submit-button "Find")
(link-to "/downloads" "Downloads"))))
Note that I've named the checkboxes "category[]", because I want their
values to be placed in a vector. (A current limitation of the
wrap-nested-params middleware that's applied by compojure.handler/site
is that any parameter with multiple values needs to end with a "[]").
When we submit the form, we wind up with a parameter map that looks a
little like:
:params {:a "foobar", :categories ["Cat1" "Cat3"]}
Which shows us that Cat1 and Cat3 were selected. The next problem is
how to turn this into a sequence of true/false values:
(defn category-checks [selected]
(->> all-categories
(map (set selected))
(map #(if % 1 0))
(apply str)))
This might require a little bit of explanation. We start with a list
of all categories, and then we want to know which categories are
selected. If we convert the selected values into a set, then we can
use the set as a function; we get a "truthy" value if the item is in
the set, or a "falsey" value (nil) if it is not.
After that, we can map over an if statement that converts true to 1
and false to 0, and then apply a str to the list of 0s and 1s so we
get our final value. This can then be used to create the URL:
(defn create-manga-url [selected-cats]
(str "
http://www.mysite.net/search/?tfield=&check="
(category-checks selected-cats)))
(POST "/" [categories]
(view-output categories))
- James
> --
> You received this message because you are subscribed to the Google Groups "Compojure" group.
> To post to this group, send email to
comp...@googlegroups.com.
> To unsubscribe from this group, send email to
compojure+...@googlegroups.com.
> For more options, visit this group at
http://groups.google.com/group/compojure?hl=en.
>