skip to content

How do you build a nested Rails layout, such as a museum staff layout that reuses application.html.erb, with content_for and render template:?

level: seniorimportance: should knowfreq 24%

answer

  1. inner layout fills regions
  2. then renders the outer layout
  3. render template: "layouts/application"
  4. content_for?(:content) ? yield(:content) : yield
  5. regions set before the outer render

basics

~20 s

In Rails, an inner layout such as layouts/staff.html.erb fills regions with content_for, typically :content and :head, then calls render template: "layouts/application"; the outer layout prints content_for?(:content) ? yield(:content) : yield, so it serves both plain and nested pages.

solid answer

~40 s

Rails has no `extends` keyword for layouts, so nesting is built from `content_for`. The inner layout, `layouts/staff.html.erb`, wraps the page in the staff sidebar inside `content_for :content do ... end`, adds anything for the head with `content_for :head`, and ends with `<%= render template: "layouts/application" %>`. The outer layout prints `content_for?(:content) ? yield(:content) : yield`, so ordinary pages, which never fill `:content`, still render through the bare `yield`. Inside the inner layout a bare `yield` still returns the action's template. The regions must be filled before the outer template is rendered, and each extra level repeats the pattern with a new region name.

code

erb · 16 lines
erb
<%# app/views/layouts/staff.html.erb %>
<% content_for :head do %>
  <meta name="robots" content="noindex">
<% end %>

<% content_for :content do %>
  <div class="staff">
    <%= render "staff/sidebar" %>
    <section><%= yield %></section>
  </div>
<% end %>

<%= render template: "layouts/application" %>

<%# app/views/layouts/application.html.erb (body excerpt) %>
<main><%= content_for?(:content) ? yield(:content) : yield %></main>

go deeper

for a junior

Recognise that one layout can render another and that content_for passes the inner parts outward.

for a middle

Write the inner layout, including the order of content_for calls and render template:, and the conditional yield in the outer layout.

for a senior

Choose between nesting, optional regions and shared partials, and debug double-printed or missing content from the render order.

for a principal

Keep the shell hierarchy shallow and its region names a documented contract, so new sections reuse it instead of forking layouts.

## The problem nested layouts solve A museum site has a public layout, `application.html.erb`, with the doctype, the `<head>`, the header and the footer. The staff area needs the same shell plus a sidebar of collection tools and a different body class. Copying the whole application layout into `staff.html.erb` would mean every change to the head has to be made twice. **Nested layouts** (also called sub-layouts) let the staff layout add its parts and then hand off to the application layout. ## The pattern 1. The **outer** layout makes its main slot overridable: `<%= content_for?(:content) ? yield(:content) : yield %>`. 2. The **inner** layout fills regions and renders the outer one: - `content_for :head` for extra tags; - `content_for :content do ... end` wrapping the sidebar and a bare `yield` (the action's template); - finally `<%= render template: "layouts/application" %>`. 3. Controllers in the staff area use the inner layout, for example through `layout "staff"` on `Staff::BaseController`. What happens at render time: the action's template renders first; Rails then renders `layouts/staff`, whose `content_for` calls store regions and whose bare `yield` reads the template output; `render template:` then renders `layouts/application` inside the same view flow, which finds `:content` filled and prints it instead of the bare template. ## Why the outer layout needs the conditional | Page | `:content` filled? | Outer layout prints | |---|---|---| | public exhibit page | no | bare `yield`, the action's template | | staff page through `staff.html.erb` | yes | `yield(:content)`, the sidebar plus template | Without the conditional, either public pages would lose their body or staff pages would print the template twice. ## Rules that keep it working - **Fill before you render.** Every `content_for` in the inner layout must run before `render template: "layouts/application"`; anything written after it is too late, because the outer layout has already printed its regions. - **Mind appending.** If a page also writes `content_for :head`, the inner layout's call appends to it. That is usually what you want for head tags, and why `:content` should be a name pages do not use. - **Use `render template:`, not `render partial:`.** The outer layout is a full template; rendering it as a template keeps its normal lookup and avoids a partial's leading-underscore naming. - **Go deeper by repeating.** A third level, say a conservation layout inside staff, fills a new region such as `:staff_content` and renders `layouts/staff`, whose `content_for :content` block prints `content_for?(:staff_content) ? yield(:staff_content) : yield`. There is no fixed limit, but each level costs a name and a little indirection. ## Alternatives worth weighing - **Shared partials** for the head and header in two separate layouts: simpler to read, at the cost of two files that can drift. - **One layout with optional regions**, using `content_for?(:sidebar)` to switch the staff sidebar on: no nesting at all, which suits small differences. - **Components** for the shell pieces, if the team already uses ViewComponent or Phlex. Nesting earns its place when the outer shell is large and changes often while the inner variants differ in structure rather than in a class name or two. ## Testing it An integration test per shell keeps the hierarchy honest: request a staff page and assert that both the staff sidebar and the application header are present with `assert_select`, then request a public page and assert the sidebar is absent. A broken conditional yield shows up immediately as a missing body or a doubled one. ## Debugging With `annotate_rendered_view_with_filenames` on in development, the HTML shows `BEGIN`/`END` comments for `layouts/staff` and `layouts/application`, which makes it easy to see which level printed what.

  • Inside `staff.html.erb`, why does the bare `yield` still return the action's page rather than the application layout?
    The action's template was rendered first and stored as the unnamed region; a bare `yield` in any layout of that render reads it. `render template: "layouts/application"` runs later in the same view flow, so the staff layout's `yield` has already captured the page into `:content` by then.
  • When would you choose one layout with `content_for?(:sidebar)` over a nested staff layout?
    When the difference is small, such as an optional sidebar or a body class, one layout with optional regions avoids a second file and the nesting indirection. Nesting pays off when the staff shell differs in structure and several controllers share it.

  • Rails layouts support an extends keyword that nests them automatically.
  • content_for calls placed after render template: still reach the outer layout.
  • The outer layout can print yield(:content) alone without a fallback.
  • Nested layouts must be rendered with render partial: "layouts/application".
  • Rails limits layouts to two levels of nesting.