lee

lee

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.

Showing Posts 1 to 8

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.

lee

lee OP

…I think myself I would prefer it to be explicit and in the generated code, as you’ve suggested.

Agreed. I found it slightly out of step with other generated code.

For example, this being the default :browser pipeline:

pipeline :browser do
  plug :accepts, ["html"]
  plug :fetch_session
  plug :fetch_flash
  plug :protect_from_forgery
  plug :put_secure_browser_headers
end

… makes it really simple for a newbie like me to see, at a glance, exactly what’s provided out-the-box.

If I want to disable or add anything new, it’s clear where to make the change.

Whereas layouts being handled by default outside of generated code, obfuscates behind a bit of background magic that wasn’t immediately clear how to override.

I wound up figuring out it was just a simple :put_layout, false (thanks @hlx for simplifying my superfluous private func), but that feels a bit like monkey-patching the core.

From my very limited/early impression, I would have preferred to see an explicit :put_response, {LayoutView, "app.html"} in generated code, so I could instead comment it out rather than override.

A small detail, but just some feedback on something that stumped me on first glance.

lee

lee OP

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

Apologies, a series of typos! That should be something along the lines of

I created a second plug in each controller, for overriding the default layout

lee

lee OP

That’s completely fair; I just expected it to wind up in generated code, like pipelines, sockets, routing, etc.

hlx

hlx

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:

# contollers/page_controller.ex
def MyAppWeb.PageController do
  use MyAppWeb, :controller

  plug :put_layout, {MyAppWeb.PageLayoutView, :page}

  def index()
end

See: Phoenix.Controller — Phoenix v1.8.8

hlx

hlx

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

lee

lee OP

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!

— All posts loaded —

Where Next? Top

Trending in Questions Top

water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
peck
Hi everyone. My team and I have been working on a fairly modest app based around video streaming and chat, but we’ve landed a customer t...
New
bradley
I really like the adapter patterns that ecto, nebulex, waffle, etc. use and would love find something similar for a key management servic...
New
unaware8150
Hello folks! So at work, we are seeing some situations where we have to define some “fixed” strings that are used across the codebase in...
New
Alvinkariuki
How Can I Optimise Compile Time Dependencies I have been building an elixir application for about 2 years now. Many modules and files ha...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New

Other Trending Topics Top

KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
juhalehtonen
There has been a thread to discuss the Stack Overflow Developer Survey on this forum every year since 2018, so here’s yet another one for...
New
budgie
I love Elixir. It’s one of 2 programming languages I’ve ever fallen in love with. But I don’t use it anymore. Serverless was the promis...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews