phoenix-framework - 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!
解决方案
推荐阅读
- python-sphinx - 编辑 Sphinx Alabaster jinja 模板
- javascript - 渲染新的 html 后,Javascript 库无法正确加载
- javascript - 使用 jest-next-dynamic 测试动态导入
- django - gcloud 构建 Django 网站的提交导致错误“没有 storage.objects.get 访问”
- c# - 在 C# 中制作一个 SAPI DLL,我有一个启动语音的函数,但我需要一个函数来检查状态。我怎么做?
- r - 为什么下面代码1中的结果?四舍五入到底什么时候发生?
- bash - 如何将带有负数作为名称的图像转换为动画/视频?
- python - 在 PyCharm 中创建新项目后出现“无效 Python SDK”错误
- c++ - 在 SFML 上检测到多个按键,无法以任何其他方式解决,并且在不同机器上的工作方式不同
- javascript - 如何将 Leaflet.Snap 添加到 Angular(11) 项目