How can I merge multiple templates in only one "view" in Phoenix 1.7?

So, in Pheonix 1.7 a controller PageController needs a PageHTML file which can use embed_templates/2.

My question is: I have multiple pages that are inside a “context” called Researcher, like Profile or Reports. In this new structure, the recommended would be the following:

  • controllers
    • researcher_profile_controller.ex
    • researcher_reports_controller.ex
  • templates
    • researcher_profile_html
      • show.html.heex
    • researcher_profile_html.ex
    • researcher_reports_html
      • show.html.heex
      • create.html.heex
    • researcher_reports_html.ex

I would like to do:

  • controllers
    • researcher_controller.ex
  • templates
    • researcher_html
      • profile.html.heex
      • create_report.html.heex
      • show_report.html.heex
    • researcher_html.ex

Is is even possible? If yes, How I could router these templates from this unique controller and “view”?

In your controller, you can specify which template to render e.g.
render(conn, :show_profile, ...)

defmodule HelloWeb.HelloController do
  use HelloWeb, :controller

  def show(conn, %{"messenger" => messenger}) do
    render(conn, :show, messenger: messenger)

In order for the render/3 function to work correctly, the controller and view must share the same root name (in this case Hello), and the HelloHTML module must include an embed_templates definition specifying where its templates live. By default the controller, view module, and templates are collocated together in the same controller directory. In other words, HelloController requires HelloHTML, and HelloHTML requires the existence of the lib/hello_web/controllers/hello_html/ directory, which must contain the show.html.heex template.

render/3 will also pass the value which the show action received for messenger from the parameters as an assign.

source: rendering section of the Phoenix Controllers docs