UNPKG

2.24 kBMarkdownView Raw
1#EE-Templates
2Templating integration for Nunjucks (Middleware)
3
4##Middleware
5The middleware can be hooked into the application stack and will append a `render(status, headers, data, callback)` method
6to the original http response (or any kind of response which adheres to the interface defined for response in `ee-webserver`).
7The renderer method contains a renderer resolved based on the accept headers, which renders the passed content into an
8appropriate representation for the `http` protocol (properly supperted at the moment: `text/html` or `application/json`).
9
10##Renderers
11Currently there are only two supported renderers: HTML and JSON. The environments (`nunjucks`) are application specific
12and need to be passed to the middleware.
13
14###HTMLRenderer
15The `HTMLRenderer` is a wrapper for nunjucks. It creates a new environment based on the domain of the request and loads
16the template which has to be set on the request (in our case resolved by the `rewriting` middleware).
17
18###DefaultRenderer
19The `DefaultRenderer` uses `JSON.stringify` and converts the passed data to a string.
20
21##Templating Extensions
22The templating extensions originally provided by this package were moved to their own packages `ee-soa-extension-api` and
23`ee-soa-extension-locale`. Since environments now are passed into the middleware, the dependency is removed and extensions
24are directly attached by the running application.
25
26##Todo
27
28 - Add a better loader which allows loading the templates asynchronously
29 - Improve handling of the mapping between the accept header and the renderer.
30
31##Media Types
32Further readings
33
34 - [Internet Media Types](http://en.wikipedia.org/wiki/Internet_media_type)
35 - [RFC 2616](http://tools.ietf.org/html/rfc2616#page-100)
36 - [MDN: Content-Negotiation](https://developer.mozilla.org/en-US/docs/Web/HTTP/Content_negotiation)
37
38##Changelog
39
40### v0.2.0
41
42 - the `render` method of the response now directly writes the data back to the original response
43 - the renderers now directly write their assigned content type to the response
44 - the renderers directly create server errors if the rendering created an error
45 - therefore the API has slightly changed from `render(data, callback)` to `render(status, headers, data, callback)`
\No newline at end of file