thousandsofthem

thousandsofthem

How to add layout to 404 page in phoenix?

ErrorView which is used to handle errors doesn’t provide conn or stuff like that. Is there a way to attach layout anyway? Showing 404 error without any layout in current project is no-go.

The only way i see at this moment is to copy all the markup from app.html.eex which is less than ideal solution.

defmodule AppWeb.ErrorView do
  use AppWeb, :view

  def render("404.html", assigns) do
    render("404.html", assigns)
  end
...

P.S. Anything else, like redirect on 404/error is not possible as well

Marked As Solved

stefanchrobot

stefanchrobot

I think you’re looking for render_layout/4.

Also Liked

c4710n

c4710n

Setting it in config.exs would be fine.

For example:

config :demo, DemoWeb.Endpoint,
  render_errors: [
    accepts: ~w(html json),
    root_layout: {DemoWeb.LayoutView, :error},
    layout: {DemoWeb.LayoutView, :skip},
    view: DemoWeb.ErrorView,
  ],
  # ...

{DemoWeb.LayoutView, :error} corresponds to lib/demo_web/templates/layout/error.html.heex.

{DemoWeb.LayoutView, :skip} corresponds to an almost empty template (lib/demo_web/templates/layout/skip.html.heex):

<%= @inner_content %>

You may find that I’m using {DemoWeb.LayoutView, :error}(this is a customized root layout for error pages) instead of the default root layout {DemoWeb.LayoutView, :root}. The reason is from here:

It is worth noting that we did not render our 404.html.heex template through our application layout, even though we want our error page to have the look and feel of the rest of our site. This is to avoid circular errors. For example, what happens if our application failed due to an error in the layout? Attempting to render the layout again will just trigger another error. So ideally we want to minimize the amount of dependencies and logic in our error templates, sharing only what is necessary.

thousandsofthem

thousandsofthem

Thank you. That’s what i was looking for. Hard to find :frowning:

For history, solution looks like this:

defmodule AppWeb.ErrorView do
  use AppWeb, :view

  def render("404.html", assigns) do
    Phoenix.View.render_layout AppWeb.LayoutView, "app.html", assigns do
      render("not_found.html", assigns)
    end
  end
...

Some hacking around get_flash is required though

c4710n

c4710n

config :demo, DemoWeb.Endpoint, 
  render_errors: [
    view: DemoWeb.ErrorView, 
    accepts: ~w(json),
    root_layout: false,
    layout: false
  ]

Phoenix.Endpoint has an option called render_errors, you can set root_layout or layout in config.

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43757 214
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
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
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement