Liking cljdoc? Tell your friends :D

Routes Helper Build Status Dependencies Status

Router aims to allow you to store route definitions in one place, and then use these routes with application tools like Compojure, and right through to your templating.

Usage

Router is available from Clojars. To define some routes...

(ns my.namespace
  (:use router.core))

;; First define your routes

(set-routes!
  {:home            "/"
   :blog            "/blog"
   :blog.post       "/blog/:id"})

;; Then you can use them with Compojure

(defroutes app-routes
  (GET (rte :home) [] "Show homepage"))

Fetching URLs

You can then use the url function to fetch parsed routes as URLs.

(url :blog.post
     :id 123) ; => "/blog/123"

Enlive

There is also support for using your routes with Enlive, via transformers.

(deftemplate bazzle "file.html"
  []
  ;; replace a form action
  [:form] (action :blog.post.comment
                  :id 123)
  ;; Or set an anchor URL
  [:a] (href :home))

These two helpers use the function route-attr which you can use to add arbitrary attributes to nodes.

ClojureScript

Router is also usable in ClojureScript, just specify it as a crossover namespace with cljsbuild. You can then share your routes between the server and the client with exactly the same code.

Can you improve this documentation?Edit on GitHub

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close