Ticket #448 (closed enhancement: duplicate)

Opened 11 months ago

Last modified 10 months ago

Ability to set layout on a per-controller basis

Reported by: cvonklei..@gmail.com Assigned to:
Priority: medium Milestone: The Future
Component: Merb Keywords:
Cc:

Description

I have a bunch of admin controllers in my Merb app that I want to use a different layout from my other controllers. My admin controllers should use the 'admin' layout, and the rest of my controllers should use the 'application' layout.

I can make them use the 'admin' layout by doing this:

class Admin::Users < Application
  self._layout = 'admin'
end

And that works, but I think this would be better:

class Admin::Users < Application
  layout :admin
end

For now, I've made it work like that by defining the layout method in my Application controller (from which all my other controllers inherit):

class Application < Merb::Controller
  def self.layout(name)
    self._layout = name.to_s
  end
end

Thanks to Yann KLIS on the merb-devel mailing list for showing me how to use self._layout = 'foo'.

Change History

01/13/08 12:34:42 changed by lancecarls..@gmail.com

I like the idea, if it doesn't already exist. +1

(in reply to: ↑ description ) 01/28/08 22:57:46 changed by mir..@digitalhobbit.com

This is a good idea, but I think we need to take it a step further and allow controllers to specify a method that determines the layout on a per-request basis, as in Rails. I just filed a ticket to describe the proposal: http://merb.devjavu.com/ticket/486

Therefore, I am against using the notation you propose to specify a (static) layout:

layout :admin

Instead, the following should be used to specify a static layout:

layout 'admin'

And the following could be used to specify a dynamic layout, based on the request:

layout :determine_layout

private

def determine_layout
  if some_condition? ? 'layout1' : 'layout2'
end

This is crucial for any application that targets different clients, such as mobile vs. browser, facebook, or different classes of users.

02/11/08 13:07:20 changed by shayarne..@gmail.com

  • status changed from new to closed.
  • resolution set to duplicate.