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…


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")
1 Like


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")

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
1 Like