Skip to content

Commit eb21e2c

Browse files
committed
Mention yield :head in Guides
Follow up to [#50527][] Highlight the presence of the `yield :head` pattern established by `turbo-rails` and adopted by Rails as part of [#50527][]. New applications will generate their application layout with `yield :head`, so mention that in the documentation. Also include the `yield :head` call in generated plugin layouts. [#50527]: #50527
1 parent 9ad3685 commit eb21e2c

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

guides/source/layouts_and_rendering.md

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,7 +1054,7 @@ Within the context of a layout, `yield` identifies a section where content from
10541054
<head>
10551055
</head>
10561056
<body>
1057-
<%= yield %>
1057+
<%= yield %>
10581058
</body>
10591059
</html>
10601060
```
@@ -1064,15 +1064,17 @@ You can also create a layout with multiple yielding regions:
10641064
```html+erb
10651065
<html>
10661066
<head>
1067-
<%= yield :head %>
1067+
<%= yield :head %>
10681068
</head>
10691069
<body>
1070-
<%= yield %>
1070+
<%= yield %>
10711071
</body>
10721072
</html>
10731073
```
10741074

1075-
The main body of the view will always render into the unnamed `yield`. To render content into a named `yield`, you use the `content_for` method.
1075+
The main body of the view will always render into the unnamed `yield`. To render content into a named `yield`, call the `content_for` method with the same argument as the named `yield`.
1076+
1077+
NOTE: Newly generated applications will include `<%= yield :head %>` within the `<head>` element of its `app/views/layouts/application.html.erb` template.
10761078

10771079
### Using the `content_for` Method
10781080

@@ -1091,15 +1093,15 @@ The result of rendering this page into the supplied layout would be this HTML:
10911093
```html+erb
10921094
<html>
10931095
<head>
1094-
<title>A simple page</title>
1096+
<title>A simple page</title>
10951097
</head>
10961098
<body>
1097-
<p>Hello, Rails!</p>
1099+
<p>Hello, Rails!</p>
10981100
</body>
10991101
</html>
11001102
```
11011103

1102-
The `content_for` method is very helpful when your layout contains distinct regions such as sidebars and footers that should get their own blocks of content inserted. It's also useful for inserting tags that load page-specific JavaScript or CSS files into the header of an otherwise generic layout.
1104+
The `content_for` method is very helpful when your layout contains distinct regions such as sidebars and footers that should get their own blocks of content inserted. It's also useful for inserting page-specific JavaScript `<script>` elements, CSS `<link>` elements, context-specific `<meta>` elements, or any other elements into the `<head>` of an otherwise generic layout.
11031105
11041106
### Using Partials
11051107
@@ -1398,7 +1400,7 @@ Suppose you have the following `ApplicationController` layout:
13981400
<head>
13991401
<title><%= @page_title or "Page Title" %></title>
14001402
<%= stylesheet_link_tag "layout" %>
1401-
<style><%= yield :stylesheets %></style>
1403+
<%= yield :head %>
14021404
</head>
14031405
<body>
14041406
<div id="top_menu">Top menu items here</div>
@@ -1413,9 +1415,11 @@ On pages generated by `NewsController`, you want to hide the top menu and add a
14131415
* `app/views/layouts/news.html.erb`
14141416

14151417
```html+erb
1416-
<% content_for :stylesheets do %>
1417-
#top_menu {display: none}
1418-
#right_menu {float: right; background-color: yellow; color: black}
1418+
<% content_for :head do %>
1419+
<style>
1420+
#top_menu {display: none}
1421+
#right_menu {float: right; background-color: yellow; color: black}
1422+
</style>
14191423
<% end %>
14201424
<% content_for :content do %>
14211425
<div id="right_menu">Right menu items here</div>

railties/lib/rails/generators/rails/plugin/templates/app/views/layouts/%namespaced_name%/application.html.erb.tt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
<%%= csrf_meta_tags %>
66
<%%= csp_meta_tag %>
77

8+
<%%= yield :head %>
9+
810
<%%= stylesheet_link_tag "<%= namespaced_name %>/application", media: "all" %>
911
</head>
1012
<body>

0 commit comments

Comments
 (0)