You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
Copy file name to clipboardExpand all lines: guides/source/layouts_and_rendering.md
+15-11Lines changed: 15 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -1054,7 +1054,7 @@ Within the context of a layout, `yield` identifies a section where content from
1054
1054
<head>
1055
1055
</head>
1056
1056
<body>
1057
-
<%= yield %>
1057
+
<%= yield %>
1058
1058
</body>
1059
1059
</html>
1060
1060
```
@@ -1064,15 +1064,17 @@ You can also create a layout with multiple yielding regions:
1064
1064
```html+erb
1065
1065
<html>
1066
1066
<head>
1067
-
<%= yield :head %>
1067
+
<%= yield :head %>
1068
1068
</head>
1069
1069
<body>
1070
-
<%= yield %>
1070
+
<%= yield %>
1071
1071
</body>
1072
1072
</html>
1073
1073
```
1074
1074
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.
1076
1078
1077
1079
### Using the `content_for` Method
1078
1080
@@ -1091,15 +1093,15 @@ The result of rendering this page into the supplied layout would be this HTML:
1091
1093
```html+erb
1092
1094
<html>
1093
1095
<head>
1094
-
<title>A simple page</title>
1096
+
<title>A simple page</title>
1095
1097
</head>
1096
1098
<body>
1097
-
<p>Hello, Rails!</p>
1099
+
<p>Hello, Rails!</p>
1098
1100
</body>
1099
1101
</html>
1100
1102
```
1101
1103
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.
1103
1105
1104
1106
### Using Partials
1105
1107
@@ -1398,7 +1400,7 @@ Suppose you have the following `ApplicationController` layout:
1398
1400
<head>
1399
1401
<title><%= @page_title or "Page Title" %></title>
1400
1402
<%= stylesheet_link_tag "layout" %>
1401
-
<style><%= yield :stylesheets %></style>
1403
+
<%= yield :head %>
1402
1404
</head>
1403
1405
<body>
1404
1406
<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
Copy file name to clipboardExpand all lines: railties/lib/rails/generators/rails/plugin/templates/app/views/layouts/%namespaced_name%/application.html.erb.tt
0 commit comments