Masatoshi Nishiguchi
Posted on February 13, 2021
I made a custom awesome_title_tag
view helper function because I wanted to support both controller views and live views.
View
In lib/mnishiguchi_web/views/layout_view.ex
defmodule MnishiguchiWeb.LayoutView do
use MnishiguchiWeb, :view
@site_name "Mnishiguchi"
def site_name(), do: @site_name
def awesome_title_tag(conn, assigns) do
live_title_tag(page_title(conn, assigns) || @site_name, suffix: " 路 #{@site_name}")
end
# live view
defp page_title(_conn, %{page_title: page_title} = _assigns) do
page_title
end
# controller view
defp page_title(conn, assigns) do
if function_exported?(view_module(conn), :page_title, 2) do
view_module(conn).page_title(view_template(conn), assigns)
else
@site_name
end
end
end
Template
In /lib/mnishiguchi_web/templates/layout/root.html.leex
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<%= csrf_meta_tag() %>
- <%= live_title_tag assigns[:page_title] || "Home", suffix: " 路 Masatoshi Nishiguchi" %>
+ <%= awesome_title_tag(@conn, assigns) %>
Resources
Because of breaking changes between Phoenix 1.4 and 1.5, many examples out there did not work with Phoenix 1.5.
Here are some resources I referenced:
- Page title via live view route that鈥檚 compatible with existing code in regular views?
- @view_template meaning changed in phoenix 1.5
- Phoenix Live View 0.10.0 Backwards incompatible changes
That's it.
馃挅 馃挭 馃檯 馃毄
Masatoshi Nishiguchi
Posted on February 13, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.