Skip to content

Instantly share code, notes, and snippets.

@mhuebert
Last active March 3, 2016 16:40
Show Gist options
  • Save mhuebert/0660003c82a39838c355 to your computer and use it in GitHub Desktop.
Save mhuebert/0660003c82a39838c355 to your computer and use it in GitHub Desktop.
defroutes macro for ClojureScript/Secretary - quickly define many routes at once
(defmacro defroutes [& routes]
`(do
~@(map (fn [[[path] body]]
(let [has-args? (or (vector? (first body)) (map? (first body)))
body (if has-args? body (cons [] body))]
`(~'secretary.core/defroute ~path ~@body)))
(partition 2 (partition-by string? routes)))))
(defroutes
"/" (if (signed-in?)
(set-view! :user/home)
(set-view! :app/landing))
"/sign-in" (set-view! :auth/sign-in)
"/sign-out" (do (auth/sign-out)
(h/nav! "/"))
"/horse/:name" [name] (prn "This horse has a name: " name) ;; parameter destructuring works normally
"*" (set-view! :app/not-found))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment