Skip to content

Commit

Permalink
First part of #49 -- allow :config :render-types to override
Browse files Browse the repository at this point in the history
Doesn't provide runtime variable rendering but it allows additional
render types to be added as well as overriding existing ones. Documents
`render-data` to make this "supported" (required for custom types).
  • Loading branch information
seancorfield committed Oct 30, 2016
1 parent 7f24d21 commit 16aaeab
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ Any controller function also has access to the the FW/1 API (after `require`ing
* `(redirecting? rc)` - returns `true` if the current request will redirect, i.e., `(redirect rc ...)` has been called.
* `(reload? rc)` - returns `true` if the current request includes URL parameters to force an application reload.
* `(remote-addr rc)` - returns the IP address of the remote requestor (if available). Checks the `"x-forwarded-for"` header (set by load balancers) then Ring's `:remote-addr` field.
* `(render-data rc type data)` or `(render rc status type data)` - low-level function to tell FW/1 to render the specified `data` as the specified `type`, optionally with the specified `status` code. Prefer the `render-xxx` convenience functions that follow is you are rendering standard data types.
* `(render-xxx rc data)` or `(render-xxx rc status data)` - render the specified `data`, optionally with the specified `status` code, in format _xxx_: `html`, `json`, `raw-json`, `text`, `xml`.
* `(rendering? rc)` - returns `true` if the current request will render data (instead of a page), i.e., `(render-xxx rc ...)` has been called.
* `(ring rc)` - returns the original Ring request.
Expand Down Expand Up @@ -163,6 +164,7 @@ as a map (preferred) or as an arbitrary number of inline key / value pairs (lega
* `:password` - specify a password for the application reload URL flag, default `"secret"` - see also `:reload`.
* `:reload` - specify an `rc` key for the application reload URL flag, default `:reload` - see also `:password`.
* `:reload-application-on-every-request` - boolean, whether to reload controller, view and layout components on every request (intended for development of applications).
* `:render-types` - an optional map of data types to replace or augment the built-in data rendering. For each data type (keyword), the value is a map of `:type` (the `Content-Type` to return) and `:body` processing function. Each function takes two arguments: the FW/1 configuration and the data to render. By default, the `:html`, `:raw-json`, and `:text` data types have a `:body` function that just returns the `data` argument as-is.
* `:routes` - a vector of hash maps, specifying route patterns and what to map them to (full documentation coming in due course).
* `:selmer-tags` - you can specify a map that is passed to the Selmer parser to override what characters are used to identify tags, filters
* `:suffix` - the file extension used for views and layouts. Default is `"html"`.
Expand Down
18 changes: 10 additions & 8 deletions src/framework/one.clj
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,6 @@
(meta #'selmer.filters/add-filter!))
(deref #'selmer.filters/add-filter!))

;; render data support
(defn render-data
([rc as expr]
(assoc rc ::render {:as as :data expr}))
([rc status as expr]
(assoc rc ::render {:status status :as as :data expr})))

;; FW/1 base functionality - this is essentially the public API of the
;; framework with the entry point to create Ring middleware being:
;; (fw1/default-handler) - returns Ring middleware for your application
Expand Down Expand Up @@ -125,6 +118,15 @@
(or (get-in rc [::ring :headers "x-forwarded-for"])
(get-in rc [::ring :remote-addr])))

(defn render-data
"Tell FW/1 to render this expression as the given type.
Prefer the convenience functions below, unless you are rendering
custom data types."
([rc as expr]
(assoc rc ::render {:as as :data expr}))
([rc status as expr]
(assoc rc ::render {:status status :as as :data expr})))

(defn render-html
"Tell FW/1 to render this expression (string) as-is as HTML."
([rc expr]
Expand Down Expand Up @@ -354,7 +356,7 @@
content type and the data rendered as the body."
[config {:keys [status as data]
:or {status 200}}]
(let [renderer (render-types as)]
(let [renderer ((merge render-types (:render-types config)) as)]
{:status status
:headers {"Content-Type" (:type renderer)}
:body ((:body renderer) config data)}))
Expand Down

0 comments on commit 16aaeab

Please sign in to comment.