router.core

expand-routes

(expand-routes routes)

Accepts a vector of routes as the argument. Returnes the expanded version of routes that can be passed to url->map and map->url functions.

Elements in the route vector must be string (pattern) or vectors that contain the string pattern and default values for that route.

(def route ":page")
;; This route will not be matched by an empty string

(def route-with-defaults [":page", {:page "index"}])
;; This route will match an empty string and the :page key will hold 
;; the value "index"

(expand-routes [[":page" {:page "index"}]
                ":page/:action"])
;; "" will be matched as {:page "index"}
;; "foo/bar" will be matched as {:page "foo" :action "bar"}

map->url

(map->url expanded-routes data)

Accepts expanded-routes vector (returned by the expand-routes function) and a map as arguments. Returns a URL part which is the closest representatation of the data contained in the map (based on the expanded-routes argument).

;; define routes
(def routes [[":page", {:page "index"}]
              ":page/:id"
              ":page/:id/:action"]) 

(def expanded-routes (expand-routes routes))

(map->url expanded-routes {:page "foo"})
;; "foo"

(map->url expanded-routes {:page "foo" :id 1})
;; "foo/1"

(map->url expanded-routes {:page "foo" :id 1 :action "bar" :qux "baz"})
;; "foo/1/bar?qux=baz"

url->map

(url->map expanded-routes url)

Accepts expanded-routes vector (returned by the expand-routes function) and a string as arguments. Returns a map which contains the data represented by the route.

;; define routes
(def routes [[":page", {:page "index"}]
              ":page/:id"
              ":page/:id/:action"]) 

(def expanded-routes (expand-routes routes))

(url->map expanded-routes "foo")
;; {:page "foo"}

(url->map expanded-routes "foo/1")
;; {:page "foo" :id 1}

(url->map expanded-routes "foo?bar=baz")
;; {:page "foo" :bar "baz"}