Implement nested layouts with Haml

91 views
Skip to first unread message

Manuel Meurer

unread,
May 31, 2014, 7:55:45 AM5/31/14
to ha...@googlegroups.com
I found this great post explaining how you can get simple nested layouts in Rails:

# app/helpers/layouts_helper.rb
module LayoutsHelper
  def parent_layout(layout)
    @view_flow.set(:layout, output_buffer)
    output = render(:file => "layouts/#{layout}")
    self.output_buffer = ActionView::OutputBuffer.new(output)
  end
end
# app/views/layouts/base.html.erb
<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
    <%= yield %>
  </body>
</html>
# app/views/layouts/application.html.erb
<div class="container">
  <%= yield %>
</div>
<% parent_layout "base" %>


You will end up with

<!DOCTYPE html>
<html>
  <head>
  </head>
  <body>
    <div class="container">
      <%= yield %>
    </div>
  </body>
</html>

Now, this works with ERB. But when I change the application.html.erb to application.html.haml, the outer layout (base) is not rendered anymore.

I tried to use `@haml_buffer` instead of `self.output_buffer` in the `LayoutsHelper` but couldn't get it to work.
Here is a repo with a vanilla Rails 4.1.1 app implementing this: https://github.com/krautcomputing/nested-layout-test

Can anyone help me to get this to work?
What do I need to change in the `LayoutsHelper`?
Thanks a lot!

Reply all
Reply to author
Forward
0 new messages