How do I implement functionality similar to Rails' url_for with Clojure and its web frameworks?

I am developing a web application with Clojure, currently with Ring , Mustache , Sandbar and Hiccup . I have a resource called job and a route to show a specific step in a multi-step form for a specific job defined this way (other routes are omitted for simplicity):

(def web-app
  (moustache/app
   ;; matches things like "/job/32/foo/bar"
   :get [["job" id & step]
         (fn [req] (web.controllers.job/show-job id step))]))

In the view that my controller displays, there are links to other steps as part of the same work. Currently, these URLs are created manually, for example. (str "/job/" id step). I don't like this encoded "/job/"portion of the URL because it repeats what I defined in the mustache route; if I change the route, I need to change my controller, which is a closer connection than I need.

I know that the Rails routing system has methods for generating URLs from parameters, and I would like for me to have similar functionality, that is, I would like to have a function url-forthat I could name as follows:

(url-for :job 32 "foo" "bar")
; => "/job/32/foo/bar"

Is there a Clojure web framework that makes this easier? If not, what are your thoughts on how this can be implemented?

+5
2

Noir . url-for.

+4

, , , . , , .

(defn url-for [& rest]
    (reduce 
        #(str %1 "/" %2) "" (map #(if (keyword? %1) (name %1) (str %1)) rest)))
+2

All Articles