Render different template files based on assigns (LiveView 0.18)

I would like to render different template files based on some values in assigns. By default my liveview is named MaintenanceWeb.MainLive.Menu in menu.ex and as such renders the template file menu.html.heex. The menu structure based on the URI path is dynamic and quite complex and as such I did not define it in router.ex except for live "/*path", MainLive.Menu.

I would like to customize the chosen file by defining a render function in my liveview but I don’t know what I need to put inside of it to get a proper Phoenix.LiveView.Rendered struct since I do not use Phoenix.View. I guess using ~H would not do the trick.

Previously, in another PoC project, I had used the following code with Phoenix.View:

  def render(assigns) do
    case assigns.live_action do
      :index -> LVWeb.LVView.render("lv_live.index.html", assigns)
      :site -> LVWeb.LVView.render("lv_live.site.html", assigns)
      :turbine -> LVWeb.LVView.render("lv_live.turbine.html", assigns)
      _ -> LVWeb.LVView.render("lv_live.index.html", assigns)
    end
  end

with function components and embed_templates you can simply call the function :index -> lv_live_index(assigns) :slight_smile:

1 Like

Thanks. I think I need to deep dive into “Programming Phoenix LiveView” and into the doc :slight_smile: