<commit_before>---
layout: "post"
author: "<NAME>"
title: "SEO Tags In Phoenix"
comments: true
---

Public facing websites need to have some basic search engine optimization (SEO) tags, such as `<title>` and `<meta name="description">`. In Rails, you could achieve this pretty simply by putting a `yield :head` tag in the appropriate layout:

```erb
<head>
  <%= yield :head %>
</head>
```

Inside any of your views, you could then use `content_for` to populate this section:

```erb
<% content_for :head do %>
  <title>My Awesome Site</title>
  <meta name="description" content="..." />
<% end %>

<!-- More view content here... --->
```

This is nice, in that it keeps the HTML all together in more or less the same place. In [Phoenix][phoenix], this is actually a bit more difficult to do elegantly.

## Why It's Hard

In Phoenix, your layout is rendered _before_ your page template is rendered. Take this layout, for example:

```erb
<html>
  <head>
    <title></title>
    <meta name="description" content="" />
  </head>
  <body>
    <%= render @view_module, @view_template, assigns %>
  </body>
</html>
```

Because Phoenix treats templates as functions, by the time your `@view_template` is rendered, the `<head>` is already rendered. So, there's nothing that your view template can do to inject content back up into `<head>`. The `content_for` approach won't work.<commit_msg>Copy change<commit_after>---
layout: "post"
author: "<NAME>"
title: "SEO Tags In Phoenix"
comments: true
---

Public facing websites need to have some basic search engine optimization (SEO) tags, such as `<title>` and `<meta name="description">`. In Rails, you could achieve this pretty simply by putting a `yield :head` tag in the appropriate layout:

```erb
<head>
  <%= yield :head %>
</head>
```

Inside any of your views, you could then use `content_for` to populate this section:

```erb
<% content_for :head do %>
  <title>My Awesome Site</title>
  <meta name="description" content="..." />
<% end %>

<!-- More view content here... --->
```

This is nice, in that it keeps the HTML all together in more or less the same place. In [Phoenix][phoenix], this is actually a bit more difficult to do elegantly.

## Why It's Hard

In Phoenix, your layout is rendered _before_ your page template is rendered. Take this layout, for example:

```erb
<html>
  <head>
    <title></title>
    <meta name="description" content="" />
  </head>
  <body>
    <%= render @view_module, @view_template, assigns %>
  </body>
</html>
```

Because Phoenix renders templates as functions, by the time your `@view_template` is rendered, the `<head>` is already rendered. So, there's nothing that your view template can do to inject content back up into `<head>`. The `content_for` approach won't work.
{'fraction_non_alphanumeric': 0.13362224629830263, 'fraction_numerical': 0.0, 'mean_word_length': 4.091911764705882, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2965039', 'n_tokens_mistral': 858, 'n_tokens_neox': 806, 'n_words': 374}