Created
May 17, 2020 16:49
-
-
Save rosswilson/a1c67aac28afb63150557d4d2e8f0229 to your computer and use it in GitHub Desktop.
Phoenix Framework Per-View Page Titles
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MyAppWeb.Admin.AccountView do | |
use MyAppWeb, :view | |
def render("page_title", assigns) do | |
page_title(view_template(assigns.conn), assigns) | |
end | |
defp page_title("index.html", _assigns), do: "Accounts" | |
defp page_title("new.html", _assigns), do: "New Account" | |
defp page_title("edit.html", _assigns), do: "Edit Account" | |
defp page_title("show.html", assigns), do: assigns.account.name | |
defp page_title(_view_template, _assigns), do: nil | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="utf-8"/> | |
<meta http-equiv="X-UA-Compatible" content="IE=edge"/> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"/> | |
<title><%= page_title(@conn, assigns) %></title> | |
<link rel="stylesheet" href="<%= Routes.static_path(@conn, "/css/app.css") %>"/> | |
<script defer type="text/javascript" src="<%= Routes.static_path(@conn, "/js/app.js") %>"></script> | |
</head> | |
<body> | |
<main role="main" class="container"> | |
<p class="alert alert-info" role="alert"><%= get_flash(@conn, :info) %></p> | |
<p class="alert alert-danger" role="alert"><%= get_flash(@conn, :error) %></p> | |
<%= @inner_content %> | |
</main> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule MyAppWeb.LayoutView do | |
use MyAppWeb, :view | |
def page_title(conn, assigns) do | |
if page_title = render_existing(view_module(conn), "page_title", assigns) do | |
page_title <> " | MyApp" | |
else | |
"MyApp" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment