For some controllers and actions I want to be able to render a custom template instead of the default one, if a custom template exists. For instance, if template/article/custom.index.html.eex exists for the route /articles/, the layout should load the custom.index.html.eex instead. Otherwise, load the default one - index.html.eex
Note that in each Controller and each action the Elixir code should remain unchanged, preferably:
render(conn, "<index, edit, show, etc...>.html")
Namely, I want to program that on the level of the layout.
render("index.html", assigns) do
try do
render_template("custom.index.html", assigns)
rescue
FunctionClauseError -> render_template("index.html", assigns)
end
end
It’s not the most performant for sure, but the question is rather would it matter. That’s you need to test with your expected load. Other solutions would require phoenix to build in such a fallback mechanism, as all the macro logic doesn’t really allow you to inject your own code between handling of different cases.