Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

layout chaining

#2162

This began as a mistake in (the Layouts and Rendering in Rails guide)[http://guides.rubyonrails.org/la...] where the author expected two layout calls in a controller to have their behaviour chained together.

I thought that made a lot of sense, so instead of a patch for the docs, I've attached a patch for action_controller/layout.rb with accompanying tests.

Reported by JasonKing · March 7th, 2009 @ 07:55 AM

State: wontfix
Milestone: 2.x
Assigned to: Yehuda Katz (wycats) Yehuda Katz (wycats)
Importance: none

Activity

  1. José Valim
    José Valim

    This won't break current applications?

    For example, if I do in my ApplicationController:

    class ApplicationController < ActionController::Base

    layout 'default'
    
    

    end

    And then:

    class AdminController < ApplicationController

    layout 'admin'
    
    

    end

    Since you are using inheritable array I'm almost sure that this will nest the layout, right?

    And I can't see also any use case for this feature.

    March 7th, 2009 @ 09:42 AM

  2. JasonKing
    JasonKing

    That use case doesn't change. The last layout called is the one that takes precedence, and because the last layout called has no conditions then it will be used for all actions on AdminController.

    Here's a use case showing the effect of this patch:

    
    class FoosController < ApplicationController
      layout 'products'
      layout 'login', :only => 'login'
      layout 'forum', :only => 'forum'
    end
    

    Right now this will just cause confusion because actually both :only conditions are stored, but only the 'forum' layout will ever be rendered.

    With my chaining patch, the behaviour will be far more as one would expect, with each condition only applying to corresponding template, and chaining back through the layouts until it finds one that applies for the called action.

    March 7th, 2009 @ 11:46 AM

  3. José Valim
    José Valim

    I think now I got it. The name totally confused me as I thought that you would nest one layout inside the other (which makes no sense). Maybe you could change the title? :)

    March 7th, 2009 @ 02:21 PM

  4. Ryan Angilly
    Ryan Angilly

    I haven't ran this patch, but +1 for concept

    March 7th, 2009 @ 05:32 PM

  5. Jeremy Olliver
    Jeremy Olliver

    Sounds like a good idea to me too.

    The patch doesn't apply cleanly to the current master though, could you update the patch with the latest master?

    March 8th, 2009 @ 07:33 AM

  6. JasonKing
    JasonKing

    Email notifications aren't working for me - sorry for the delay :(

    Just rebased, should be good now.

    March 11th, 2009 @ 05:51 PM

  7. Matt Jones
    Matt Jones

    As this has been punted to the next release, I've updated the guide to correctly reflect the current behavior.

    March 12th, 2009 @ 07:09 AM

  8. CancelProfileIsBroken
    CancelProfileIsBroken
    • Assigned user set to Yehuda Katz (wycats)

    Any thoughts on how this will behave in 3.0? We shouldn't make this change on 2.x unless 3.0 will behave the same way.

    May 16th, 2009 @ 05:21 PM

  9. Yehuda Katz (wycats)
    Yehuda Katz (wycats)

    I really don't like this idea at all. It looks like another example of moving app-specific logic with very uncommon use-cases into a core location in the framework. Every such innocuous change adds more complexity to the overall system, including parts of Rails that are only tangentially related.

    Why could this not be achieved with an in-app helper that captured the content with its appropriate layout and then using that capture in the layout. I'd personally be much more amenable to a helper that made that sort of thing easier than in adding more complexity to the already non-trivial layout system.

    May 16th, 2009 @ 05:46 PM

  10. CancelProfileIsBroken
    CancelProfileIsBroken
    • State changed from new to wontfix

    The helper solution seems more likely to work cleanly in both 2.x and 3.0 if you want to pursue it.

    May 16th, 2009 @ 05:51 PM

  11. JasonKing
    JasonKing

    Have you guys seen my second comment in this ticket?

    Where multiple layout statements with conditions will have their conditions combined, but the template itself is overwritten.

    There are two options that I can see to solve this, one was the layout chaining idea that I was investigating in this patch, the other is to ensure that previous conditions are cleared/overwritten when processing the layout call.

    This issue shouldn't be closed until one of those is done.

    May 16th, 2009 @ 11:23 PM

  12. bingbing