Serving different content formats from single controller's action

Looking for a way to distinguish whether e. g. document.html or document.pdf was requested and to serve appropriate content format in return. Something akin to respond_to in that other framework. I think I solved it already before but I can’t recall now. Hints/pointers appreciated.

https://hexdocs.pm/phoenix/Phoenix.Controller.html#module-rendering-and-layouts

Yes, I read this one but still don’t feel like this answers it, especially in the context of “viewless” Phx, which is what everybody believes to be the future, man :wink: Also don’t recall making multiple “Views” for that purpose but if there are no other answers then maybe memory doesn’t serve

The need for multiple modules comes from the fact that function components normalized to arity-1 functions (NameSpaceFormat.name(assigns)), which receive only assigns. There’s no additional parameter anymore, which would pass the format to the function transforming assigns to the format being rendered. Switching by format now happens on the view/layout/root layout level with put_view/put_layout/put_root_layout and them being updated to accept keyword lists.

Phoenix.View had render("name.format", assigns), which could switch on format within the same module. If you want to retain that you can keep using Phoenix.View by manually depending on it.

In regards to “viewless phx” I’m not sure what you’re refering to. Yes Phoenix.View is no longer used, but you can and likely will have modules serving the purpose of views still. Personally I’m a fan of phoenix having changed to dumping those modules next to controllers though, as it makes that fact less discoverable. I guess the tldr there would be: On a high level nothing changed (views are still a thing), but the implementation changed.

1 Like

Ah, that would explain. So this in fact changed, which might be the reason why in the recent docs I couldn’t find how I think I did it previously. TNX