Explicitly specify a different co-located template?

For example, if I wanted to conditionally render a co-located template. e.g. if the socket is connected.

page_live.html.leex
page_live_connected.html.leex

Is there any way to specify one explicitly? Something like how one can specify a normal template? Phoenix.View.render(MyAppWeb.PageView, "page.html", assigns)

Hey, this is an old question, but It got my attention since the workaround we were applying looks like it is not going to be the recommended approach in Phoenix 1.7.

So, now it is possible to specify the template you want to render selecting one template, using the render function from the View module, for example:

  def render(%{connected: true} = assigns) do
    MyAppWeb.PageView.render("page_live_connected.html.heex", assigns)
  end

  def render(assigns) do
    MyAppWeb.PageView.render("page_live.html.heex", assigns)
  end

But, from Phoenix 1.7.0, it is recommended to avoid the usage of views and templates folders. This means we should keep rendering a co-located template.

So, which is the recommended approach for conditionally rendering one template or other for LV pages?

You can do the same you did before with function components on MyAppWeb.PageView.

Not sure what you mean. With Phoenix 1.7, the Views module will be removed, right?

So, you mean using the co-located functions components on the HTML module? Similarly to what is going to happen for controllers, that automatically looks for a function component for each action, as it is explained:

But instead of the controller calling AppWeb.UserView.render("index.html", assigns) , we’ll now first look for an index/1 function component on the view module, and call that for rendering if it exists. Additionally, we also renamed the inflected view module to look for AppWeb.UserHTML

Phoenix.View will be removed as in the library/module which handled the API of view templates (all the render(template, assigns)). Modules holding function components compiled from template files aren’t going away.