Forum: Ruby on Rails adding layouts within layouts

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Sam D. (Guest)
on 2006-05-23 06:48
(Received via mailing list)
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.
Ben and Kaz A. (Guest)
on 2006-05-23 07:25
(Received via mailing list)
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
Steve R. (Guest)
on 2006-05-23 09:47
(Received via mailing list)
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+layout...
Sent from the RubyOnRails Users forum at Nabble.com.
Ben and Kaz A. (Guest)
on 2006-05-23 09:59
(Received via mailing list)
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
Guest (Guest)
on 2006-05-23 10:06
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
Ingo W. (Guest)
on 2006-05-23 10:49
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:

...
<title><%= @content_for_title %></title>
...
<body><%= @content_for_body %></body>
...

Hope this helps,
Ingo
Michael C. (Guest)
on 2006-05-24 05:47
(Received via mailing list)
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:

<html>
	<head>...</head>
	<body>
		<div id="header">...</div>
		<div id="left-junk">...</div>
		<div id="content-area"><%= @content_for_layout %></div>
		<div id="footer">...</div>
	</body>
</html>

But @content_for_layout would be the output of another layout:

<div id="submenu">...</div>
<div id="content"><%= @content_for_layout %></div>

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/
Sam D. (Guest)
on 2006-05-24 06:21
(Received via mailing list)
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.
Ezra Z. (Guest)
on 2006-05-24 07:01
(Received via mailing list)
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
>                <div id="header">...</div>
>
> "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:
<html>
  <body>
   <div id='header></div>
   <div id='content'><%= @content_for_layout %></div>
   <div id='sidebar'><%=  yield :sidebar %></div>
   <div id='footer'></div>
  </body>
</html>


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

<% content_for :sidebar do %>
    <h1>I'm content for the sidebar!</h1>
<% 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
seth b. (Guest)
on 2006-06-02 11:48
(Received via mailing list)
Ok where the hell did you pick _that_ one up EZ? I even missed it before
now...
Steve R. (Guest)
on 2006-06-08 21:32
(Received via mailing list)
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
  "<title>#{SITE_NAME}#{" : #{@page_title}" if @page_title}</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):

<title><%= yield :page_title %></title>

Any suggestions for implementing the conditional inclusion of the colon?

TIA
--
View this message in context:
http://www.nabble.com/adding-layouts-within-layout...
Sent from the RubyOnRails Users forum at Nabble.com.
seth b. (Guest)
on 2006-06-20 01:11
(Received via mailing list)
Add a global helper...?
This topic is locked and can not be replied to.