Skip to content

Commit

Permalink
added live_meta_title_tag
Browse files Browse the repository at this point in the history
  • Loading branch information
dustessavdh committed Jan 11, 2022
1 parent 1aa9630 commit ae2e6ef
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
47 changes: 47 additions & 0 deletions lib/hergetto_web/helpers/meta_tags.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,51 @@ defmodule HergettoWeb.Helpers.MetaTags do
end)
end

@doc """
Renders a meta title tag with automatic prefix/suffix on `@page_title` updates.
## Examples
<%= live_meta_title_tag assigns[:page_title] || "Welcome", prefix: "MyApp – " %>
<%= live_meta_title_tag assigns[:page_title] || "Welcome", suffix: " – MyApp" %>
"""
def live_meta_title_tag(title, opts \\ []) do
meta_title_tag(title, opts[:prefix], opts[:suffix], opts)
end

defp meta_title_tag(title, nil = _prefix, "" <> suffix, _opts) do
[
tag(:meta, name: "title", content: title <> suffix),
tag(:meta, name: "og:title", content: title <> suffix),
tag(:meta, name: "twitter:title", content: title <> suffix)
]
end


defp meta_title_tag(title, "" <> prefix, nil = _suffix, _opts) do
[
tag(:meta, name: "title", content: prefix <> title),
tag(:meta, name: "og:title", content: prefix <> title),
tag(:meta, name: "twitter:title", content: prefix <> title)
]
end

defp meta_title_tag(title, "" <> pre, "" <> post, _opts) do
[
tag(:meta, name: "title", content: pre <> title <> post),
tag(:meta, name: "og:title", content: pre <> title <> post),
tag(:meta, name: "twitter:title", content: pre <> title <> post)
]
end

defp meta_title_tag(title, _prefix = nil, _postfix = nil, []) do
[
tag(:meta, name: "title", content: title),
tag(:meta, name: "og:title", content: title),
tag(:meta, name: "twitter:title", content: title)
]
end

defp meta_title_tag(_title, _prefix = nil, _suffix = nil, opts) do
raise ArgumentError,
"live_meta_title_tag/2 expects a :prefix and/or :suffix option, got: #{inspect(opts)}"
end
end
2 changes: 1 addition & 1 deletion lib/hergetto_web/live/login_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule HergettoWeb.LoginLive do

@impl true
def mount(_params, session, socket) do
{:ok, fetch(socket, session)}
{:ok, fetch(socket, session) |> assign(page_title: "Login")}
end

def fetch(socket, session) do
Expand Down
2 changes: 2 additions & 0 deletions lib/hergetto_web/templates/layout/root.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<%= csrf_meta_tag() %>

<%= live_meta_title_tag assigns[:page_title] || "Hergetto", suffix: " ⋅ Watch videos together!" %>
<%= meta_tags(assigns[:meta_attrs] || %{}) %>

<%= live_title_tag assigns[:page_title] || "Hergetto", suffix: " ⋅ Watch videos together!" %>
Expand Down

0 comments on commit ae2e6ef

Please sign in to comment.