我想在我的Selmer模板中访问当前页面的URL,以便我可以将其传递给编辑页面操作,这样即使在编辑之后,此页面也可以包含一个返回到“呼叫”页面的链接。
下面是我的Selmer模板中的模板代码--看起来没问题:
<a href="/photos/_edit/{{p.path}}{% if back %}?back={{back}}{% endif %}"
class="btn btn-warning btn-sm">edit</a>下面是我在搜索时设置返回值的方法:
(defn photo-search [word req] (layout/render "search.html" {:word word :photos (db/photos-with-keyword-starting word) :back (str (:uri req) "?" (:query-string req)) })) ;; ... (defroutes home-routes ;; ... (GET "/photos/_search" [word :as req] (photo-search word req))
这个可以正常工作。然而,我还有其他返回照片列表的方法,将此代码添加到所有其他方法中似乎违反了DRY原则。
有没有一种更简单的方法来做这件事,也许是使用一些中间件?
发布于 2017-04-23 07:41:27
您可以尝试的一种方法是创建自己的render函数,该函数包装selmer,并在每个页面上提供您想要的通用功能。类似于:
(defn render
[template request data]
(let [back (str (:uri req) "?" (:query-string req))]
(layout/render template (assoc data :back back))))
(defroutes home-routes
(GET "/photos/" [:as req]
(->> {:photos (db/recent-photos)}
(render "list.html" req)))
(GET "/photos/_search" [word :as req]
(->> {:word word
:photos (db/photos-with-keyword-starting word)}
(render "search.html" req))))(出于某些原因,我真的很喜欢在路由中使用线程宏,尽管它们在线程中的链接可能不足以证明它是合理的……)
https://stackoverflow.com/questions/43557404
复制相似问题