作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我最近刚刚在玩 Compojure,我有一个小的基本 web 应用程序。对于我的 HTML 模板,我使用的是 Enlive,并且我有一个包含所有简单静态页面的命名空间。这些页面的 defroute 调用如下所示:
(defroutes public-routes
(GET "/" []
(info/index-template))
(GET "/about" []
(info/about-template))
(GET "/contact" []
(info/contact-template)))
(defroutes info-routes
(map #(GET (str "/" %) [] (ns-resolve 'webapp.pages.info
(symbol (str % "-template"))))
'("about" "contact")))
最佳答案
您可以随时使用 routes
defroutes 使用的函数:
(defroutes info-routes
(apply routes
(map #(GET (str "/" %) []
(ns-resolve 'webapp.pages.info
(symbol (str % "-template"))))
'("about" "contact"))))
(defn templates-for [& nss]
(->> nss
(map ns-publics)
(apply concat)
(filter #(->> % first str
(re-seq #"-template$")))
(map second)))
(defn template-uri [template]
(->> template meta :name name
(re-seq #"(.*)-template$")
first second (str "/")))
(defn template->route [template]
(GET (template-uri template) [] template))
(defroutes public-routes
(GET "/" [] "foo")
(apply routes (map template->route
(templates-for 'webapp.pages.info))))
templates-for
function 将在给定的命名空间中查找以“-template”结尾的任何函数,并用它们编写适当的路由。看看我没有使用任何宏,而是使用大量组合。
关于clojure - 从列表创建 Compoju 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5812117/
我最近刚刚在玩 Compojure,我有一个小的基本 web 应用程序。对于我的 HTML 模板,我使用的是 Enlive,并且我有一个包含所有简单静态页面的命名空间。这些页面的 defroute 调
我是一名优秀的程序员,十分优秀!