Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Phoenix can implicitly render responses based off Accept header #7

Open
th0mas opened this issue Jul 13, 2020 · 1 comment
Open

Phoenix can implicitly render responses based off Accept header #7

th0mas opened this issue Jul 13, 2020 · 1 comment
Labels

Comments

@th0mas
Copy link

th0mas commented Jul 13, 2020

Phoenix can already implicitly render different responses based off the Accept header or through forcing a type through the put_accept plug.

def index(conn, _params) do
  doors = Access.list_doors()
  render(conn, :index, doors: doors)
end

Will render "index.json" or "index.html" implicitly.

See https://gist.github.com/chrismccord/31340f08d62de1457454.

In the Phoenix Docs: https://hexdocs.pm/phoenix/Phoenix.Controller.html#render/3

Specifically;

template - which may be an atom or a string. If an atom, like :index, it will render a template with the same format as the one returned by get_format/1. For example, for an HTML request, it will render the "index.html" template. If the template is a string, it must contain the extension too, like "index.json"

@nelsonic
Copy link
Member

Yeah, we read that Gist and it makes sense.
However we weren't able to get it working consistently in our demo app.
Also, we didn't want to have to create a .json template for each route handler.
If you can demo it based on the Accept header, please share. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants