The easiest method is given as default I believe.
Router Method
Router:
live "/dashboard", DashboardLive.Index, :index
live "/team", DashboardLive.Index, :home
DashboardLive.Index
--- Default Handle Params ---
def handle_params(params, _, socket) do
{:noreply,
socket
|> apply_action(socket.assigns.live_action, params)
}
end
--- Actions Matching Your Router Atoms ---
defp apply_action(socket, :index, params) do
socket
|> assign(:page_title, "Index")
end
defp apply_action(socket, :home, params) do
socket
|> assign(:page_title, "Home")
end
Index.html.heex
<%= if @live_action in [:index] do %>
--- Content For Index ---
<% end %>
<%= if @live_action in [:home] do %>
--- Content For Homr ---
<% end %>
For the above, the apply_action values of :index and :home within the DashboardLive.Index file are paired with the paths in your Router.
live "/dashboard", DashboardLive.Index, :index
When you go to /dashboard you run:
defp apply_action(socket, :index, params) do
socket
|> assign(:page_title, "Dashboard")
end
The handle_params allows you to use <%= if @live_action in [:whatever] %> to display content specific to the Router path as it applies the action to the socket.
Non Router Method
If you want to display content without using different router paths, you can still do similar.
In your mount, assign a default value to the socket.
def mount(_params, _session, socket) do
socket = socket
|> assign(:page, "dashboard")
{:ok, socket}
end
You can display any dashboard based content by using:
<%= if @page === "dashboard" do %>
<% end %>
To change the content, when a button is clicked you trigger a handle_event and update the socket value for page instead of changing path
Home button on the dash.
<.button type="button" phx-click="change content" phx-value-page="home" >
<%= "Home Button %>
</.button>
Handle_Event to update the socket. The “page” => page will take your phx-value-page string value and assign it to the socket as :page. Effectively you are change :page from “dashboard” to “home”
def handle_event("change conten", %{"page" => page}, socket) do
{:noreply, socket |> assign(:page, page)}
end
Once the socket has been updated the @page value will now be “home” so the original content being displayed as “dashboard” will be hidden, and the below will be displayed.
<%= if @page === "home" do %>
<% end %>
The handle_event may need something like patch to reload the content though, not sure how my example actually performs as I obviously didn’t test it.
I’m just a humble amateur
but I believe the Router method is prefered for full page loads, and the non Router method is more for showing/hiding things on the page or passing values around.