What's the most elegant way to use specific html render on liveview?


I’m building a different dashboard for each user role on my app.
What is the best approach in this case?

For example:
I have a dashboard for admin, so I would like to render dashboard_admin.html
For a user with a role vendor I would like to render dashboard_vendor.html and so on…
I tried live_component but I don’t think it would be a better way and render different templates I had no success because in this version it seems like the macro view doesn’t exist anymore.

I’m using Phoenix 1.7 and Liveview 0.18.3.
Any thoughts about that would be really appreciate. =)

Thank you.

live_session macro in your router.

1 Like

Some recent discussion here.

1 Like

I created a live session but as the path is equal for the same dashboard and the only think that changes is the HTML I got stuck on first match on route for route home (~p"/") others routes with this same path that I was targeting different dashboard was ignored because of that.

Sorry If I am wrong but I think that is a little bit different what I’m trying to do.
I will use the same layout (sidebar, navbar, etc…) for all dashboards.

Ah, you can either put the common stuff in root.html.heex or extract it to a layout component and use it like so in your different layouts:

<.layout user={@current_user} menu_items={@menu_items}>
  <h1>Admin Dashboard</h1>

You can load @menu_items using a on_mount/4, much like how @current_user is loaded.

1 Like

Thanks for your suggestion.

At end I used for this feature Live Component and shared between different users on platform.

Thanks guys.