2017-04-22 61 views
0

我想訪問我的Selmer模板中的當前頁面URL,以便我可以將它傳遞給編輯頁面操作,以便即使在編輯之後,此頁面也可以包含返回「調用」頁面的鏈接。簡單的方法來訪問每個selmer模板中的請求映射?

下面是我的塞爾默模板模板代碼 - 這似乎OK:

<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原則將此代碼添加到所有其他方法。

有沒有更簡單的方法來做到這一點,也許有一些中間件?

回答

0

您可以嘗試的一種方法是創建自己的render函數,該函數包裝selmer's並在每個頁面上提供所需的常用功能。喜歡的東西:

(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)))) 

(出於某種原因,我真的很喜歡使用線程宏在路線,即使他們可以說是不夠的鏈接在線程的理由吧...)

相關問題