lee

lee

Disabling layouts by default

I’m new to Phoenix (and Elixir in general); I’m coming to Phoenix from a Javascript background (I’m the author of ReactQL, to give you an idea on the typical stack I’m used to.), so forgive me if the naïve/novice query.

One thing that stumped me immediately in Phoenix was the implicit templates/layout/app.html.eex. I’m developing a pretty typical app which is chunked into several sections - a public front-end, the user app itself, an admin area, an API. Having a default central layout doesn’t serve the requirements of this app, so I spent the first little while trying to figure out how to disable it.

What I wound up with was just a plug in the :browser pipeline, pointing to a private function that disables the default layout. I then deleted templates and layout_view.ex. Finally, in my controllers, I created a second plug for specifying routes connections to that particular controller.

Whilst this was fairly trivial to do (and I’m not even sure that’s the only - or right - way to go about it), the notion of first disabling the layout and then setting a new one seems a bit like a code smell to my novice Phoenix nose. In other frameworks I’ve worked with, layouts don’t tend to be assumed until explicitly set; I’ve found they’re often not required.

So for my introductory question to this board (and thanks in advance for helping to further my currently very basic understanding of Phoenix!), I’m curious what prompted the design decision to make layout/app.html.eex (and LayoutView) an implicit choice? My first instinct was to search Phoenix generated code to look for a specific reference to “app.html”, assuming that I’d just need to comment out the default line to prevent it… but it looks like LayoutView is hard-coded in lib code.

Again, I’m new here so I may be missing something obvious, but this did strike me as a tad opinionated and I’m curious what may have led to it.

Most Liked

hlx

hlx

Phoenix allows you to changes pretty much anything you want and I think most of the projects require an layout so they made it default.

You could do it like this:

# router.ex
pipeline :no_layout do
  plug: :put_layout, false
end

scope "/api", MyAPI do
  pipe_through [:browser, :no_layout]
  ( ... )
end

I fail to understand what you mean by this? Maybe the scope and extra pipeline make the second plug not needed anymore?

lpil

lpil

Creator of Gleam

I would say that in general Phoenix is an opinionated framework that makes a lot of opinionated decisions in order to provide a particular experience that fits the majority of web apps.

Whether this is the best way if a matter of personal preference. I think myself I would prefer it to be explicit and in the generated code, as you’ve suggested.

hlx

hlx

I have to agree, having the plug :put_layout, {MyApp.LayoutView, :app} generated by default in the router would be nicer

Last Post!

lee

lee

I feel I need to mention this in case you haven’t thought of it, in your controller you can also simply call put_layout/1 instead of using a separate plug for it:

Thanks @hlx, I thought of that after seeing your original example disabling it – appreciated!

Where Next?

Popular in Questions Top

vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New

Other popular topics Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40082 209
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement