3

In a regular controller/view, let's say I have AppWeb.ItemView, and AppWeb.ItemController. Let's say I have two different indexes, :index and :index_funky. If I wanted to render index.html.eex for the :index_funky view, I could create a render function in AppWeb.ItemView with the head render("index_funky.html", assigns) and inside that do a render("index.html", assigns). That works fine: I would get the data from AppWeb.ItemController.index_funky using the template from AppWeb.ItemController.index.

How can I do the same with a LiveView? If I have AppWeb.ItemLive.Index and AppWeb.ItemLive.IndexFunky, how can I render index.html.leex for AppWeb.ItemLive.IndexFunky?

Brendon
  • 879
  • 1
  • 6
  • 18

1 Answers1

2

Not sure about your reason behind it, but I would encourage you to look at handle_params if it helps your case.

Now, for your case, you can delegate to an existing view like this

defmodule AppWeb.ItemLive.IndexFunky do
  use AppWeb, :live_view

  def render(assigns) do
    Phoenix.View.render(AppWeb.ItemLive.Index, "index.html", assigns)
  end
end 

Documentation

lusketeer
  • 1,890
  • 1
  • 12
  • 29
  • 1
    As for reasoning, continuing with my fake example, `:index` would be all `Items`, whereas `:index_funky` is just `Items` that have the `funky` attribute. Exact same attributes being displayed in a table, but just a subset of the data. It's very duplicative to copy the template for several views, especially when I also want to display an `:index_groovy` and an `:index_nifty` as well. – Brendon Jun 06 '20 at 13:21
  • @Brendon yeah, handle_params should do the trick, you can even just do a `live_path` link for :funky, :nifty, :groovy, and let the handle_params to assign the correct result to socket – lusketeer Jun 06 '20 at 14:54
  • take a look at this example where handle_params is used, https://elixirschool.com/blog/live-view-live-component/ – lusketeer Jun 06 '20 at 14:55