How should you use nested layouts?

Technology CommunityCategory: Ruby on RailsHow should you use nested layouts?
VietMX Staff asked 3 years ago

The layouts removes code duplication in view layer. You are able to slice all your application pages to blocks such as header, footer, sidebar, body and etc.

A generated Rails application has a default layout and it’s defined in the app/views/layouts/application.html.erb. On the screen above there is only one dynamic block – it’s the body, the footer, the header and the sidebar are common blocks for each page. So the code for the layout may look like this:

<!DOCTYPE html>
<html>
<head>
</head>
<body>
  <%= render 'shared/header' %>
  <%= render 'shared/sidebar' %>
  <%= yield %>
  <%= render 'shared/footer' %>
</html>

The yield in the code above is the place in which any action template will be rendered. This is a default Rails behavior.