首页 > 解决方案 > Phoenix different title per page with root module

问题描述

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!

标签: phoenix-frameworkphoenix-live-view

解决方案


推荐阅读