Adding layouts within layouts


#1

Hi,

I want to be able to have several layouts within each other so that I
don’t
have to keep repeating the layout code for one, in another. An example
would be having a layout for the title of your web page and then having
an
embedded layout for the body portion in which the HTML for whatever view
gets displayed. Is this possible?

Thanks.


#2

On 5/23/06, Sam D. removed_email_address@domain.invalid wrote:

I want to be able to have several layouts within each other so that I don’t
have to keep repeating the layout code for one, in another. An example
would be having a layout for the title of your web page and then having an
embedded layout for the body portion in which the HTML for whatever view
gets displayed. Is this possible?

Tell me if I I’m teaching you how to suck eggs, but think of layouts
as containers for your views, and partials as components that you can
reuse in your layouts and views. So you could create partials for
each of the elements that you wish to reuse in each layout to achieve
the desired result without trying to reuse layouts within layouts.

cheers,
Ben


#3

On 5/23/06, s.ross removed_email_address@domain.invalid wrote:

I believe components are close to being deprecated. Having used them, I find
them interesting in concept but suboptimal in performance. My solution has
been to use partials for reusing portions a a view (and they’re cacheable!)
and code in /lib for any real wiring.

Anyone else know the status of components?

I should have been more careful with my words. I wasn’t referring to
rails ‘components’, but was using the english word component to
describe a ‘partial’. I think we’re on the same wavelength about
using ‘partials’ as reusable portions in ‘views’ and ‘layouts’.

cheers,
Ben


#4

Ben and Kaz A. wrote:

On 5/23/06, s.ross removed_email_address@domain.invalid wrote:

I believe components are close to being deprecated. Having used them, I find
them interesting in concept but suboptimal in performance. My solution has
been to use partials for reusing portions a a view (and they’re cacheable!)
and code in /lib for any real wiring.

Anyone else know the status of components?

I should have been more careful with my words. I wasn’t referring to
rails ‘components’, but was using the english word component to
describe a ‘partial’. I think we’re on the same wavelength about
using ‘partials’ as reusable portions in ‘views’ and ‘layouts’.

cheers,
Ben

This is test posting for Ruby on Rails


#5

I believe components are close to being deprecated. Having used them, I
find
them interesting in concept but suboptimal in performance. My solution
has
been to use partials for reusing portions a a view (and they’re
cacheable!)
and code in /lib for any real wiring.

Anyone else know the status of components?

View this message in context:
http://www.nabble.com/adding+layouts+within+layouts-t1666558.html#a4517577
Sent from the RubyOnRails Users forum at Nabble.com.


#6

Hi Sam,

I hope I understand your problem, but being quite new to Rails I was
more than a little unsure about how to efficiently organize views and
layouts in the beginning.

I am still would like to read about exactly what best practices are, but
what helped me a lot was discovering the “collect” mechanism. With this,
you can, in your views, collect content for multiple parts in your
layouts(left colum, right column, and the like), and then specify in the
layouts where the that content should go. Applied to your example (title
and body), this would look somewhat like this:

In your views:

<% content_for(‘title’) do %>
<%= render :partial => ‘title’ %>
<% end %>

<% content_for(‘body’) do %>
<%= render :partial => ‘body’ %>
<% end %>

In your layouts:

<%= @content_for_title %> ... <%= @content_for_body %> ...

Hope this helps,
Ingo


#7

I couldn’t agree more. Maybe this is possible but just don’t know how
to go
about doing this. I thought of all of your alternatives too but they
just
seemed ugly.

Thanks.


#8

On Mon, May 22, 2006 at 07:45:43PM -0700, Sam D. wrote:

Hi,

I want to be able to have several layouts within each other so that I don’t
have to keep repeating the layout code for one, in another. An example
would be having a layout for the title of your web page and then having an
embedded layout for the body portion in which the HTML for whatever view
gets displayed. Is this possible?

I ran into this badly in a project that is almost finished. I have 5
major sections to the site, each with a submenu above the content which
is shared for that section, save for the “current” page being
highlighted.

What I need is two layouts. The main layout would be:

...
...
...
<%= @content_for_layout %>
...

But @content_for_layout would be the output of another layout:

...
<%= @content_for_layout %>

That way, each section could get its own submenu layout file, and
nothing would be repeated.

As it is right now, I have repeated the submenu code in all of the
views for a particular section, which is hideous.

My other option is to create a separate layout for each one, but then
the rest of the page code would be repeated.

And the last option is to create two partials for each section: a
“header” and a “footer”. Then I could render those in every view.
That’s ugly, though, as the code would be split up.

I need to be able to nest layouts, and I believe this would be a very
useful feature. I just don’t have the internals experience to pull it
off.

Any ideas?

Thanks,
Michael

Michael Darrin Chaney
removed_email_address@domain.invalid
http://www.michaelchaney.com/


#9

Ok where the hell did you pick that one up EZ? I even missed it before
now…


#10

Hi!

On May 23, 2006, at 7:17 PM, Sam D. wrote:

I ran into this badly in a project that is almost finished. I have 5

“header” and a “footer”. Then I could render those in every view.
That’s ugly, though, as the code would be split up.

I need to be able to nest layouts, and I believe this would be a very
useful feature. I just don’t have the internals experience to pull it
off.

Any ideas?

Thanks,
Michael

Have you seen the content_for method? You can use it to push content

up to the layout from a normal view. Like so:

in the layout:

<%= @content_for_layout %>
<%= yield :sidebar %>

in any normal view file that gets rendered in the above layout

<% content_for :sidebar do %>

I’m content for the sidebar!


<% end %>

other view code.

This allows you to make placeholders in your layout that get

populated from the content_for method in your view files.

-Ezra


#11

Sorry to dredge up an old thread, but I’m retrofitting some older code.
I
have a helper:

def title_for_page
raise “Site name not set” if !SITE_NAME # make a lot of noise if no
site
name
“#{SITE_NAME}#{” : #{@page_title}" if @page_title}"
end

The intent of this code is to use the site name as the initial part of
the
title, then optionally a colon separator and the page name. I’d prefer
to
write (in layout.rhtml):

<%= yield :page_title %>

Any suggestions for implementing the conditional inclusion of the colon?

TIA

View this message in context:
http://www.nabble.com/adding-layouts-within-layouts-t1666558.html#a4777092
Sent from the RubyOnRails Users forum at Nabble.com.


#12

Add a global helper…?