Am I doing this right? (Page title to change depending on what page the user is accessing)

Hello, so I wanted the page title to change depending on what page the user was accessing…

For example: Website · Home, Website · Calculators, Website · About

Here is my code below:

<%= live_title_tag assigns[:page_title] || "Website", suffix: " · #{view_template(@conn)}" %>

I was wondering if I’m doing this right…

If you assign page_title variable it will set the title automatically in normal controllers:

def home(conn, _params) do
  opts = [page_title: "Home"]
  render("home.html", opts)
<%= live_title_tag assigns[:page_title] || "Default Title", prefix: "Website · " %>

This will generate tags like “Website · Home”.

live_title_tage/2 look at prefix and suffix options

For live views, documentation - you have to assign to sockets.

def mount(_params, _session, socket) do
    socket = assign(socket, page_title: "Latest Posts")
    {:ok, socket}

Are you using normal phoenix views or live views ?


Thank you so much.

1 Like

Phoenix Views, but I might use live views for the calculators…

1 Like