Skip to content

Instantly share code, notes, and snippets.

@tomholford
Created May 15, 2020 01:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomholford/be9fd2a49996c0358ffcb0d514167780 to your computer and use it in GitHub Desktop.
Save tomholford/be9fd2a49996c0358ffcb0d514167780 to your computer and use it in GitHub Desktop.
Extend Rails application layouts

extends

An elegant way to nest Rails layouts.

Example Usage

# devise.html.erb
<%= extends :application do %>
  <div class="devise-container">
    <%= yield %>
  </div>
<% end %>
# Add this to your application_helper.rb
# From https://github.com/rwz/nestive/blob/master/lib/nestive/layout_helper.rb
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment