reorganize 'render' functions a bit. added a 'render-response' function to allow using these in lower-level ring handlers/middleware (so all template rendering / http status handling happens in one place more or less)
This commit is contained in:
parent
1f16054710
commit
3e365757cc
|
@ -11,6 +11,7 @@
|
|||
(:require [noir.util.middleware :refer [app-handler]]
|
||||
[noir.response :as resp]
|
||||
[compojure.route :as route]
|
||||
[compojure.response :refer [render]]
|
||||
[taoensso.timbre :as timbre]
|
||||
[taoensso.timbre.appenders.rotor :as rotor]
|
||||
[selmer.parser :as parser]
|
||||
|
@ -19,7 +20,7 @@
|
|||
|
||||
(defroutes app-routes
|
||||
(route/resources "/")
|
||||
(route/not-found (layout/render "notfound.html")))
|
||||
(layout/render-handler "notfound.html" :status 404))
|
||||
|
||||
(defn init []
|
||||
(timbre/set-config!
|
||||
|
@ -52,9 +53,11 @@
|
|||
(handler request)
|
||||
(catch Throwable e
|
||||
(timbre/error e)
|
||||
{:status 500
|
||||
:headers {"Content-Type" "text/html"}
|
||||
:body (layout/render-template request "error.html" {:error-info e})}))))
|
||||
(layout/render-response
|
||||
request
|
||||
"error.html"
|
||||
:params {:error-info e}
|
||||
:status 500)))))
|
||||
|
||||
(def app (app-handler
|
||||
[auth-routes home-routes posts-routes files-routes rss-routes app-routes]
|
||||
|
|
|
@ -7,21 +7,27 @@
|
|||
|
||||
(def template-path "blarg/views/templates/")
|
||||
|
||||
(defn render-template [request template params]
|
||||
(defn- render-template [request template params]
|
||||
(parser/render-file
|
||||
(str template-path template)
|
||||
(assoc params
|
||||
:context (:context request)
|
||||
:user-id (session/get :user))))
|
||||
|
||||
(defn render-response [request template & {:keys [params status content-type]}]
|
||||
(-> (render-template request template params)
|
||||
(resp/response)
|
||||
(resp/content-type (or content-type "text/html; charset=utf-8"))
|
||||
(resp/status (or status 200))))
|
||||
|
||||
(defn render-handler [template & {:keys [params status content-type]}]
|
||||
(fn [request]
|
||||
(render-response request template :params params :status status :content-type content-type)))
|
||||
|
||||
(deftype RenderableTemplate [template params status content-type]
|
||||
Renderable
|
||||
(render [this request]
|
||||
(-> (render-template request template params)
|
||||
(resp/response)
|
||||
(resp/content-type (or content-type "text/html; charset=utf-8"))
|
||||
(resp/status (or status 200)))))
|
||||
(render-response request template :params params :status status :content-type content-type)))
|
||||
|
||||
(defn render [template & {:keys [params status content-type]}]
|
||||
(RenderableTemplate. template params status content-type))
|
||||
|
||||
|
|
Reference in a new issue