Nested Layouts in Merb

0 views
Skip to first unread message

Jacques Crocker

unread,
Nov 23, 2008, 10:09:57 PM11/23/08
to merb
Hi. I'm trying to DRY up my merb layouts. I have a overall app layout,
and then a few different inner templates that add some additional
markup wrapping. The ideal scenario for me would be to have something
like nested layouts.

Searching google I found this:
http://blog.new-bamboo.co.uk/2008/3/15/nested-layouts-in-merb

Unfortunately I dont think it works in Merb 1.0. When I try the exact
code in this technique, I get a hard merb crash. I believe its causing
an infinite loop. Heh, not only does it crash, but when I do ctrl-C to
stop the server it doesnt close it down. I have to kill the processes
manually.

Can someone take a quick look and see what they are doing wrong? Or is
there an easier way now in Merb to use nested layouts?

I'll go ahead and update the wiki with this once we're able to figure
this out. Nested layouts without crazy hacks (like the ones needed in
the rails nested layouts plugin) would be an awesome merb selling
point.

Thanks

Yehuda Katz

unread,
Nov 23, 2008, 11:44:47 PM11/23/08
to me...@googlegroups.com
The easiest way I've found to do nested layouts is:

render(render(:layout => :sublayout))

in effect, render(:layout => :sublayout) will do the render against the sublayout, and the outer layout will embed that into the default layout. You could also do:

render(render(:layout => :sublayout), :layout => :another_layout)

which would embed the rendering with "sublayout" inside "another_layout".

The reason this works is that render(:layout => :sublayout) returns a string, and render() takes a string, and embeds it into the default layout (or another layout if specified).

Does that make sense?
--
Yehuda Katz
Developer | Engine Yard
(ph) 718.877.1325

Yehuda Katz

unread,
Nov 23, 2008, 11:46:18 PM11/23/08
to me...@googlegroups.com
And I forgot to say that you build your subtemplate exactly the same as you would build a regular template, using catch_content(:for_layout).

-- Yehuda

Jacques Crocker

unread,
Nov 24, 2008, 12:30:04 AM11/24/08
to merb
Makes sense. Very cool!

Thanks Yehuda

On Nov 23, 8:46 pm, "Yehuda Katz" <wyc...@gmail.com> wrote:
> And I forgot to say that you build your subtemplate exactly the same as you
> would build a regular template, using catch_content(:for_layout).
> -- Yehuda
>
>
>
>
>
> On Sun, Nov 23, 2008 at 8:44 PM, Yehuda Katz <wyc...@gmail.com> wrote:
> > The easiest way I've found to do nested layouts is:
> > render(render(:layout => :sublayout))
>
> > in effect, render(:layout => :sublayout) will do the render against the
> > sublayout, and the outer layout will embed that into the default layout. You
> > could also do:
>
> > render(render(:layout => :sublayout), :layout => :another_layout)
>
> > which would embed the rendering with "sublayout" inside "another_layout".
>
> > The reason this works is that render(:layout => :sublayout) returns a
> > string, and render() takes a string, and embeds it into the default layout
> > (or another layout if specified).
>
> > Does that make sense?
>

Jacques Crocker

unread,
Nov 24, 2008, 1:10:12 AM11/24/08
to merb
Holy crap. This nested layouts thing is awesome...

Check out this helper:

def parent_layout(layout, &block)
render capture(&block), :layout => layout
end

Now any of my layouts can specify its parent layout via a simple block

sublayout.html.haml
= parent_layout "application" do
.sublayout
= catch_content :for_layout

Yehuda Katz

unread,
Nov 24, 2008, 1:52:46 AM11/24/08
to me...@googlegroups.com
Sweet!

One of the nice things about always returning strings is happy accidents like this :)

-- Yehuda
Reply all
Reply to author
Forward
0 new messages