I have a fairly straightforward problem, but I’m having a tricky time deciphering an answer from the documentation.
My applications has three parts, a front page, various dashboard interfaces/lobbies, and a main game page.
I’d like to have a common template for all of the dashboard pages. I thought that maybe I could use a different root.html for those pages, but the documentation suggests that this is some kind of magic value, such that I can only one root.html. So, I’d like to keep the universal root, but still have dashboard pages inherit a common template.
What is that best way for different views to share a common template other than the root? Does Phoenix provide something equivalent to Django’s {% extends 'base.html' %}?
Thanks, that’s a good starting point. But, it looks like only for non-Live views, and it doesn’t use the root.html, but rather a base template which contains all of the headers in it, meaning that those will all have to be updated independently for site-wide changes. I’d like something structured like this:
root
dashboard_base
user_settings
I have found references online to render_existing, but this appears to have been removed in a previous version. Was it replaced by anything?
You can set the root layout via put_root_layout or root_layout on the live_session:
When working with LiveViews, there are usually three layouts to be
considered:
the root layout - this is a layout used by both LiveView and
regular views. This layout typically contains the <html>
definition alongside the head and body tags. Any content defined
in the root layout will remain the same, even as you live navigate
across LiveViews. All LiveViews defined at the router must have
a root layout. The root layout is typically declared on the
router with put_root_layout and defined as “root.html.heex”
in your MyAppWeb.LayoutView. It may also be given via the :root_layout option to a live_session macro in the router.
To follow up with this, I ended up using some of the ideas outlined here, which let me use the existing universal root along with nested templates where I want them, as shared components. Worked out fine.
This is my final post about this, but I’ve come to the conclusion that my overall strategy was wrong, and it’s better to just forget the Django paradigm of templates-in-templates-in-templates entirely and instead think in terms of reusable components. Ex - rather than having a ‘dashboard’ template, instead have ‘header’, ‘sidebar’ and ‘footer’ components which you drop in a minimal scaffold. This prevents headaches with live_component and needing to access @socket in places you might not anticipate.