root
Hello everyone.
I have an app that’s going to be setting the layout dynamically according to the current domain name. I’m trying to figure out a way to do it in LiveView.
I’ve gotten to the point where I have an on_mount helper that gets the current hostname and makes it available in assigns, but I’m not able to use that from within the LiveView mount calls, because the helper depends on hooking handle_params. The only other place to set the layout (as far as I know) is in the router or in the myapp_web file where the :live_view helpers are defined, and I’m not seeing how to get access to the hostname from either of those locations.
Is there a recommended way to do this that I’ve missed?
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New
Other Trending Topics
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
sodapopcan
I’m assuming you have a fixed number of subdomains since you probably aren’t going to have an infinite number of layouts
There are a few ways to do this. My current project is an online store which has a “retail” layout (for the main domain) and an “admin” layout (for the backoffice
admin.subdomain).I’m currently doing it like this:
In
MyAppWeb, ie, inlib/my_app_web.exI have:In
lib/my_app_web/components/layouts/I add anadmin.html.heexandretail.html.heex. The:adminin{MyAppWeb.Layouts, :admin}, for example, corresponds toadmin.html.heex.Then in my LiveView, instead of
use MyAppWeb, :live_viewI douse MyAppWeb, :retail_liveanduse MyAppWeb, :admin_live.They do both share the same root layout which is very generic.
I’m happy with my solution but I’d also be interested in hearing how others do this!
sodapopcan
Sorry, I think I may have misunderstood what you’re asking. Do you have multiple domains pointing the same LiveView and want the layout to change based on that? In that case I’m not sure it’s possible as I believe the only place to set it is when calling
use LiveView, layout: {Mod, :file}. I could be wrong but can’t find anything in the docs and there is this post from José. It’s 2 years old so maybe something’s changed?There may be a way. What does your router look like?
root
I have a theoretically infinite set of domain names, not just subdomains, to support, but they map to a small, fixed set of themes.
The docs state the layout can be set from the router in the
live_session, and from themount(). I detailed why themount()approach hasn’t panned out in the OP, and I’m not sure how to get the host information I need into the router to do it from thelive_session.My router right now mostly has the admin routes defined which are exempt from this whole thing. I’ve been working on them while searching for a way to accomplish this. Nothing but a test liveview defined for the public area, and the helpers that I’ve confirmed grab the host and the configuration from my database.
I was using plugs when I had this idea working a while ago in deadviews. The same strategy hasn’t worked in 1.7+ and with LiveViews.
I’m sure there’s some way!
sodapopcan
So apart from
:temporary_assigns, it looks likemount/3also accepts a:layoutoption! So I think you could figure out the layout in youron_mountthen grab it from thesocketinmount/3and return it like:{:ok, socket, layout: socket.assigns.layout}. I haven’t actually tried this or anything but that is my best guess. The only thing is that you’ll have to repeat that for every LiveView which probably isn’t the biggest deal.root
That’s what I’m currently attempting to do, but it looks like
mount/3happens beforehandle_paramswhich is where my helper is grabbing the urlSo the information I need (the current host) isn’t yet accessible during
mount. Unless there’s another way to access it than the current strategy I found here (in a discussion about styling the active nav link)olivermt
Can you not set up a plug that puts the host in the session?
sodapopcan
Wellllllll ****. Could you add it to the session from a plug so it’s available in mount? Maybe not ideal. I don’t think I can be much help here as I’ve never actually done this myself. I’m interested, though, so hopefully someone else can help!
(and yes, I also read that same thread and how I set the current URL too
)
EDIT: Ha, re: post that beat me to the punch
root
This actually works! Thank you both! Let me clean it up a bit and make sure I understand it and then I’ll post the full working thing. Are there any downsides or ramifications I should be aware of with regards to storing this in the session? Is this session data modifiable by the user? Going through the docs now
My proof of concept right now is using
String.to_atomwhich i understand comes with some concerns, butlayout:demands an atom in Phoenix 1.7+sodapopcan
Session is definitely modifiable by user so if that’s a concern that prob won’t work. It’s further less desirable with
String.to_atomas it opens you up to an attack.The other option here is to use components instead of the layout files. You could load the layout name in
handle_paramsthen have a component that delegates to the proper layout component.then in your LiveView:
I would probably pattern match in function heads over that case statement, but you get the gist.
If you were using Surface you could use dynamic components though I’ve never used those so can’t give an example.
EDIT: Just for completeness I meant this as an alternative to the
case:sodapopcan
And to be clear I meant like they could potentially manipulate the session based on the hostname. If you are whitelisting hostnames then it’s no problem.