Skip to content

Instantly share code, notes, and snippets.

@pbmarcano
Created October 13, 2016 15:42
Show Gist options
  • Save pbmarcano/395cea9727f70542150cb89df4a5d945 to your computer and use it in GitHub Desktop.
Save pbmarcano/395cea9727f70542150cb89df4a5d945 to your computer and use it in GitHub Desktop.
Nested layouts in Rails
<!DOCTYPE html>
<html>
<head>
<%= render 'layouts/head' %>
</head>
<body>
<%= render 'layouts/navigation' %>
<div class="container">
<%= yield %>
</div>
<%= render 'layouts/footer' %>
</body>
</html>
module ApplicationHelper
#....
def extends(layout, &block)
# Make sure it's a string
layout = layout.to_s
# If there's no directory component, presume a plain layout name
layout = "layouts/#{layout}" unless layout.include?('/')
# Capture the content to be placed inside the extended layout
@view_flow.get(:layout).replace capture(&block)
render file: layout
end
#....
end
<%= extends :application do %>
<%# additional HTML... %>
<%= yield %>
<%# additional HTML... %>
<% end %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment