Clojure 从列表创建Compojure路由
我最近刚玩过Compojure,我有一个小的基本网络应用程序。对于我的HTML模板,我使用的是Enlive,我有一个名称空间来保存所有简单的静态页面。这些页面的defroute调用如下所示:Clojure 从列表创建Compojure路由,clojure,compojure,Clojure,Compojure,我最近刚玩过Compojure,我有一个小的基本网络应用程序。对于我的HTML模板,我使用的是Enlive,我有一个名称空间来保存所有简单的静态页面。这些页面的defroute调用如下所示: (defroutes public-routes (GET "/" [] (info/index-template)) (GET "/about" [] (info/about-template)) (GET "/contact" [] (info/contact-tem
(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")))
当然,这不起作用,因为映射返回的是惰性序列,而不是函数体(?)。
有人知道我需要做什么才能让这个想法发挥作用吗
或者我应该使用一种完全不同的方法来减少自己的重复吗?所以很遗憾,您无法将其传递给map之类的函数。您需要编写一个宏,将其扩展为对defroutes的调用。或者查看它扩展到的函数并直接调用它们
像这样在调用defroutes时创建路由列表是行不通的
(defroutes public-routes
(make-list-of-routes)
将展开为路线列表:
(defroutes public-routes
( (GET "/" [] (info/index-template))
(GET "/about" [] (info/about-template))
(GET "/contact" [] (info/contact-template))) )
如果defroutes
在正常函数中,您将使用apply
(apply defroutes (conj 'public-routes (make-list-of-routes)))
因为defroutes
是一个宏,它在apply运行之前就完全完成了,结果没有多大意义。你真的不能把宏写成函数。在clojure(或我所知道的任何口齿不清的语言)中,马克洛人不是一等公民
当一些Clojurians(通常不是我)说“Macroes是邪恶的”时,他们经常会想到这样的情况:当你试图编写某个宏时,你会发现它是一个宏,但却不能
解决方案是不使用defroutes宏,直接调用routes函数 您始终可以使用defroutes使用的
路由
功能:
(defroutes info-routes
(apply routes
(map #(GET (str "/" %) []
(ns-resolve 'webapp.pages.info
(symbol (str % "-template"))))
'("about" "contact"))))
但那仍然很无聊,让我们加点香料吧!;-)
使用此代码,函数的
模板将查找给定名称空间中以“-template”结尾的任何函数,并使用它们编写适当的路由。看看我是如何不使用任何宏,而是使用大量合成的。我不确定是否误解了,但我不是将宏传递给映射,而是尝试将映射的结果传递给宏。据我所知,我的函数返回一个惰性的函数序列,然后我希望defroutes来处理它。或者我遗漏了什么?按照亚瑟的建议,我们可以讨论(defroutes r a b c)
是(def r(routes a b c))
的快捷方式,其中routes
是(幸运的!)一个普通函数。您的示例以(routes[abc])
(用一个seq代替矢量,但您明白了)而不是(routes a b c)
。这可以通过应用来解决:(def r(应用路由[a b c]))
。如果routes
是一个宏,这当然不起作用。我确实遇到了一些问题,因为现在传入了一个参数,以前没有传入,但我刚刚将defsnippet参数列表修复为[\ux],现在它工作得很好。谢谢
(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))))