Hello ! I am a bit confused as to what the template arguments “index.json”, “show.json” and such reference in the render/3 function. From my understanding of the documentation ( Phoenix.Controller — Phoenix v1.6.15 ), the template argument of the render function should point to a .html file in the templates directory.
Could someone please explain what exactly this argument does ? Apologies for this stupid question, but I’m really confused.
Thank you !
Hello and welcome,
.json is for json API. You don’t need a template to render json, the view will be enough. For example…
def render("index.json", %{users: users}) do
%{data: render_many(users, UserView, "user.json")}
end
def render("show.json", %{user: user}) do
%{data: render_one(user, UserView, "user.json")}
end
def render("user.json", %{user: user}) do
%{id: user.id}
end
It is used in the view render function, for pattern matching
Thank you !