0

This question is similar to (but not an exact duplicate of) Phoenix Framework - page titles per route.

Ideally, i want to create titles like in the described question, but I am using a root layout since my project uses Phoenix LiveView. The HTML skeleton including the head and title HTML tag are part of the root template (root.html.eex). The app template extends on that from my understanding. I implemented the code from the above question

<title>
  <%= if Kernel.function_exported?(@view_module, :title, 2) do %>
    <%= @view_module.title(Phoenix.Controller.action_name(@conn), assigns) %> - StHub
  <% else %>
    StHub
  <% end %>
</title>

and created a title function inside of my specific page view

defmodule StHubWeb.WowsView do
  use StHubWeb, :view

  def title(_action, _assigns) do
    "Dashboard"
  end
end

but the else branch of the code is triggered. Upon further inspection, I think that the issue is with using a root template, because the @view_module while rendering the root template is StHubWeb.LayoutView, and only inside of the LayoutView/app.html.eex template, the @view_module is my actual view (StHubWeb.WowsView).

I am not sure how to solve this other than removing the root template, but then my LiveView will have to contain the entire HTML skeleton all the time. Maybe there is a way for me to define a title function in my LayoutView that will grab the title from StHubWeb.WowsView, but I am not sure how to do that.

Thanks for the help!

Jan
  • 1,268
  • 4
  • 12
  • 20
  • Have you seen this: https://hexdocs.pm/phoenix_live_view/Phoenix.LiveView.html#module-updating-the-html-document-title Please note that the question you link to is actually quite old. Phoenix Live View has evolved a lot since then. – Peaceful James Jun 24 '20 at 02:18
  • 1
    I generated a phoenix 1.5 project with `mix phx.new --live foo` and I see in `lib/foo_web/templates/layout/root.html.leex` there is a `live_title_tag` element which uses `:page_title` on the assigns. You just have to assign this `:page_title` in your live view to get a custom title (in PHoenix 1.5). – Peaceful James Jun 24 '20 at 02:26
  • Thanks @PeacefulJames, I have a project where I added live view to after creating the project already, so I didn't have that root template. I would prefer an option where I can export a function somewhere instead of having to assign every time, but that will work for now! thanks! – Jan Jun 24 '20 at 06:06

0 Answers0