Re: How to let the layout be more OO?


#1

Hi,

I encountered the same issue. I’m sure there are better ways to do
this, and perhaps some even “blessed” ways, but here is what I did.

In my application controller, I added the following:

before_filter :render_sidebars

private

def render_sidebars
@left_sidebar_for_layout = render_to_string(:partial =>
‘shared/left_sidebar’)
@right_sidebar_for_layout = render_to_string(:partial =>
‘shared/right_sidebar’)
end

Then, in my application layout, I use these new content variables the
same as I would for @content_for_layout.

It’s not pretty, but it works.

Anyone got a better way?

Thanks,
Brad

Bradley M.


#2

Hi,

I am just about to launch into something similar. One thing that I have
been thinking of (but havn’t had time to try yet) is to use the
content_for
method.

Using Techno-weenie’s acts_as_authenticated as a guide, the index page
contains the following

<%% content_for ‘poem’ do -%>

some text in here

<%%= simple_format @content_for_poem %>

I’m not sure why there is %% in there but it works.

What my thinking is that in your view you could include many of these
content_for ‘name’ functions
that would then be rendered in your layout.

The doc is available at
http://api.rubyonrails.org/classes/ActionView/Helpers/CaptureHelper.html#M000450

This is then available to your layouts as <%= @content_for_name %>

I hope this works… I’m going to be trying it soon :slight_smile:

Cheers
Dan