How to set the value of a meta-tag attribute by an entity's field value

I have posts and want to set some custom head meta-tags by the values from posts fields (like description, og_image etc…)

How to pass the values of posts fields up to app.html.eex?

You can pass data from controller to main layout. See…

https://www.brainarama.com/thought/dbfe9020-b431-11e7-ae34-3f710d564c96/Elixir-Phoenix-framework-pass-data-from-controller-action-to-layout-view-app-html-eex

2 Likes

Thanks for sharing @kokolegorille, that was pretty simple to achieve compared to other frameworks.

I think, you can access assings in your app.html.eex

<title><%= assings[:title] || "default title" %></title>

and pass it from your controllers

def some_controller(conn, _params) do
  render(conn, "some_template.html", title: "custom title")
end
1 Like

@idi527

Thanks, that was simpler. Based on your suggestion, this is working, in app.html.eex:

<meta name="og:image" content="<%= if assigns[:og_image] != "" do assigns[:og_image] else "/images/logo_h70.png" end %>">

and in controller:

def some_controller(conn, _params) do
  render(conn, "some_template.html", og_image: "og_image_value")
end

I don’t think it would work if there is no :og_image assign.

That is, if

assings = []
if assigns[:og_image] != "" do # nil != "", true
  assigns[:og_image] # returns nil
else
  "/images/logo_h70.png"
end
1 Like