Render content in layout

May 30, 2007 — Leave a comment

I came up with a hack to render content inside a layout, from the controller:

1
2
3
4
5
6
7
8
9
10
  def content_with_layout(content_for_layout, controller, layout = "the_layout")
    locals = { :params => controller.params }
    template = controller.class.view_class.new(controller.class.view_root, locals, controller)
    template.instance_variable_set("@content_for_layout", content_for_layout)    
    
    # This might work to get assigns? untested...
    #template.assigns = controller.template.instance_variable_get("@assigns") 

    template.render_file("layouts/#{layout}", true)    
  end  

You have to pass in any locals you want, and if you want the use assigns in your layout you would have to set the template.assigns. There was some plugin to give you extra layouts in the view but I forget what it was called. Anybody know where that is?

Update: The inside layout is at: http://fora.pragprog.com/rails-recipes/write-your-own/post/144

No Comments

Be the first to start the conversation!

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s