content v1.3.0 Content
Content
handles Content Negotiation in any Elixir/Phoenix App.
Please see: github.com/dwyl/content for detail.
Link to this section Summary
Functions
get_accept_header/1
gets the "accept" header from req_headers.
Defaults to "text/html" if no header is set.
init/1
initialises the options passed in and makes them
available in the lifecycle of the call/2
invocation (below).
When invoking the Content
plug in a Phoenix router pipeline,
we pass in a Map containing a key html_plugs
with a list of
plugs that need to be run when the accept header is "html".
See implimentation docs for more detail/clarity.
redirect_path/1
returns the conn.request_path
with the .json removed.
reply/5
gets the "accept" header from req_headers.
Verify if url finishes with .json
.
Defaults to "text/html" if no header is set.
The Content.reply/5
accepts the following 5 argument
url_json?/1
check if the current url endpoint terminate with .json
wildcard_redirect/3
redirects a "/route.json" request to "/route"
such that a request to "/admin/profile.json" invokes "admin/profile"
router
is the Phoenix Router for your app, e.g: MyApp.Router
see: https://github.com/dwyl/content#4-wildcard-routing
Link to this section Functions
call(conn, options)
call/2
is invoked to handle each HTTP request which Content
inspects.
If the accept header is "html", execute the html_plugs
for that request.
If the accept header contains "json" return the conn
unmodified.
If the url terminate with .json
return the conn
unmodified.
get_accept_header(conn)
get_accept_header/1
gets the "accept" header from req_headers.
Defaults to "text/html" if no header is set.
init(options)
init/1
initialises the options passed in and makes them
available in the lifecycle of the call/2
invocation (below).
When invoking the Content
plug in a Phoenix router pipeline,
we pass in a Map containing a key html_plugs
with a list of
plugs that need to be run when the accept header is "html".
See implimentation docs for more detail/clarity.
redirect_path(conn)
redirect_path/1
returns the conn.request_path
with the .json removed.
reply(conn, render, template, json, data)
reply/5
gets the "accept" header from req_headers.
Verify if url finishes with .json
.
Defaults to "text/html" if no header is set.
The Content.reply/5
accepts the following 5 argument:
conn
- thePlug.Conn
where we get thereq_headers
from.render/3
- thePhoenix.Controller.render/3
function, or your own implementation of a render function that takesconn
,template
anddata
as it's 3 params.template
- the.html
template to be rendered if theaccept
header matches"html"
; e.g:"index.html"
json/2
- thePhoenix.Controller.json/2
function that rendersjson
data. Or your own implementation that accepts the two params:conn
anddata
corresponding to thePlug.Conn
and thejson
data you want to return.data
- the data we want to render asHTML
orJSON
.
url_json?(conn)
url_json?/1
check if the current url endpoint terminate with .json
wildcard_redirect(conn, params, router)
wildcard_redirect/3
redirects a "/route.json" request to "/route"
such that a request to "/admin/profile.json" invokes "admin/profile"
router
is the Phoenix Router for your app, e.g: MyApp.Router
see: https://github.com/dwyl/content#4-wildcard-routing