I have a basic layout that I''m using for all views. This layout is
enhanced for specific view types by a custom include mechanism. I''d
prefer to dispense with the latter and instead cascade layouts.
That is I''d like to have a base layout, where in addition to
@content_for_layout other variables are inserted which may be defined
in view specific layouts. More concretely
layouts/base.rhtml:
<html>
<head>
...
<%= @content_for_head %>
</head>
<body>
<%= @content_for_body_top %>
<%= @content_for_layout %>
<%= @content_for_body_bottom %>
</body>
</html>
layouts/form.rhtml:
<% content_for(''head'') do %>
...
<% end %>
...
Then, in a controller, I''d like to be able to call
def edit
...
render :action => ''edit'', :layout =>
''form''
end
(or have it done automatically based on action name and :layout
specification).
I don''t see an obvious way to achieve this. Is there a non-obvious one?
Michael
--
Michael Schuerig All good people read good books
mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org Now your
conscience is clear
http://www.schuerig.de/michael/ --Tanita Tikaram, Twist In My Sobriety
Michael,
I currently do just about what you''re asking for, and I don''t
think
any of the core mods I''ve done play a role in this working for me.
The following should work (if I''ve boiled down my use of it properly
into this example):
# app/controllers/managers_controller.rb
class ManagersController < ApplicationController
def edit
end
end
# app/views/layouts/base.rhtml (I use application.rhtml to be
consistent with Rails)
<html>
<head>
<title><%= @title || "My Site" %></title>
<%= @content_for_header || '''' %>
</head>
<body>
<div id="sidebar">
<%= @content_for_sidebar || '''' %>
</div>
<div id="main">
<%= @content_for_layout %>
</div>
</body>
</html>
# app/views/layouts/managers.rhtml
<%
@content_for_sidebar = ''My sidebar''
@title = "Manager''s Page"
render ''layout/base''
%>
The only thing it seems that you''d want to change from the above is
maybe in ManagersController#edit adding a class variable @layout if
you want to parameterize which layout managers.rhtml would render.
Then in managers.rhtml having
render "layout/#{@layout || base}"
I haven''t tried that last bit though.
Hope it helps.
- Scott Reilly
OneOwl on #rubyonrails
On 8/5/05, Michael Schuerig
<michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org>
wrote:>
> I have a basic layout that I''m using for all views. This layout is
> enhanced for specific view types by a custom include mechanism.
I''d
> prefer to dispense with the latter and instead cascade layouts.
>
> That is I''d like to have a base layout, where in addition to
> @content_for_layout other variables are inserted which may be defined
> in view specific layouts. More concretely
On Saturday 06 August 2005 02:48, Scott Reilly wrote:> I currently do just about what you''re asking for, and I don''t think > any of the core mods I''ve done play a role in this working for me. > The following should work (if I''ve boiled down my use of it properly > into this example):[snip] Thanks, that does what I need. My own previous approach was actually quite similar, but I had "control inverted"; I like it better now. Michael -- Michael Schuerig Most people would rather die than think. mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org In fact, they do. http://www.schuerig.de/michael/ --Bertrand Russell