layouts per action

13 views
Skip to first unread message

dc

unread,
Aug 24, 2008, 9:11:11 AM8/24/08
to merb
hi all -

Merb has a layout method for setting html layout to use -per
controller- but is there a recommended way to get per-action layouts
(or remove layout) ?

with Rails:
layout 'blah', :except =>[:a1, a2]

I guess:

def action
render :layout => 'blah'


works but its a bit of a pain to spec the layout in each action.

tx,

/dc

Michael Klishin

unread,
Aug 25, 2008, 5:16:19 AM8/25/08
to me...@googlegroups.com
2008/8/24 dc <dc.p...@gmail.com>:

> I guess:
>
> def action
> render :layout => 'blah'
>
>
> works but its a bit of a pain to spec the layout in each action.

before :pick_layout

protected

def pick_layout
pick = ...
self.class.layout(pick)
end
--
MK

Mirko

unread,
Aug 26, 2008, 12:42:18 PM8/26/08
to merb
Actually, unless it's changed recently, you should be able to simply
do this:

layout :determine_layout

protected

def determine_layout
'foo'
end


However, I don't know if / how you can find out which action is being
processed right now, so you can use this to determine which layout to
render.

-Mirko


On Aug 25, 2:16 am, "Michael Klishin" <michael.s.klis...@gmail.com>
wrote:
> 2008/8/24 dc <dc.pik...@gmail.com>:

Michael Klishin

unread,
Aug 26, 2008, 12:49:45 PM8/26/08
to me...@googlegroups.com
2008/8/26 Mirko <mfroe...@gmail.com>:

>
> Actually, unless it's changed recently, you should be able to simply
> do this:
>
> layout :determine_layout
>
> protected
>
> def determine_layout
> 'foo'
> end

sources say you still can do that :)
--
MK

Yehuda Katz

unread,
Aug 26, 2008, 12:56:20 PM8/26/08
to me...@googlegroups.com
action_name should tell you which action is being processed.

-- Yehuda
--
Yehuda Katz
Developer | Engine Yard
(ph) 718.877.1325

dc

unread,
Aug 28, 2008, 1:30:46 PM8/28/08
to merb
thanks for the tips everyone.
FWIW we ended up adding this:


module Merb::RenderMixin
include Merb::ControllerExceptions

def layout(layout, options)
self._default_render_options ||= {}
if options[:only]
only = (options[:only].is_a?(Symbol) ? [options[:only]] :
options[:only])
self._default_render_options.update(:layout => ((layout &&
only.include?(action_name.to_sym)) ? layout : false))
elsif options[:exclude]
exclude = (options[:exclude].is_a?(Symbol) ?
[options[:exclude]] : options[:exclude])
self._default_render_options.update(:layout => ((layout && !
exclude.include?(action_name.to_sym)) ? layout : false))
else
self._default_render_options.update(:layout => (layout ?
layout : false))
end
end
end


# so now we can do:

class Profiles < Application

before lambda{|c| c.layout choose_layout, :exclude => :play}



its a bit more long-winded than i would like...
Reply all
Reply to author
Forward
0 new messages