Mathieu Chappuis
2006-Feb-02 14:06 UTC
[Rails] How switching global layout (application.rthml <or> custom.rhtml)
Dears, My railsapp run using ''/layout/application.rhtml'' and under some conditions ( Authenticated user, url parameter) I like to switch globally to another layout, let''s say custom.rhtml Like : class AppController < ApplicationController before_filter :context_layout def index end end # application.rb def context_layout if <context_condition> layout(''custom'') end end But layout() call is not available into application.rb, and writing class AppController < ApplicationController layout(''custom'') def index end end Works,but it''s impossible to add here test conditions like @params. The layout method accept a conditions={} parameters, but I dont figure out how use it. Thanks for helping Mathieu
Adam Denenberg
2006-Feb-02 14:28 UTC
[Rails] How switching global layout (application.rthml <or> custom.rhtml)
what about this # application.rb def context_layout if <context_condition> @session[:layout] = ''newlayout'' else @session[:layout] = ''oldlayout'' end end then in the top of your controllers after your before_filter do: layout "#{@session[:layout]}" On 2/2/06, Mathieu Chappuis <mathieu.chappuis.lists@gmail.com> wrote:> Dears, > > My railsapp run using ''/layout/application.rhtml'' and under some > conditions ( Authenticated user, url parameter) I like to switch > globally to another layout, let''s say custom.rhtml > > Like : > > class AppController < ApplicationController > before_filter :context_layout > > def index > end > end > > # application.rb > def context_layout > if <context_condition> > layout(''custom'') > end > end > > > But layout() call is not available into application.rb, and writing > > class AppController < ApplicationController > > layout(''custom'') > > def index > end > end > > Works,but it''s impossible to add here test conditions like @params. > > The layout method accept a conditions={} parameters, but I dont figure > out how use it. > > Thanks for helping > > Mathieu > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Mathieu Chappuis
2006-Feb-02 15:06 UTC
[Rails] How switching global layout (application.rthml <or> custom.rhtml)
Thanks Adam That''s look pretty DRY but not works.> then in the top of your controllers after your before_filter do: > > layout "#{@session[:layout]}" >Unless I''m a noob , @session or @params are NOT available on the begining of a controller. (before methods definitions) and give a nil exception. Cheers, Mathieu
James Adam
2006-Feb-02 15:12 UTC
[Rails] How switching global layout (application.rthml <or> custom.rhtml)
class ApplicationController layout :determine_layout # symbol indicates it''s a method call in this case... # ...and here''s the method def determine_layout if user_is_logged_in? "name_of_your_template" elsif params[:whatever] "name_of_another_template" else "default_template_name" # default end end end ... basically lifted from page 370 of the book. On 2/2/06, Mathieu Chappuis <mathieu.chappuis.lists@gmail.com> wrote:> Dears, > > My railsapp run using ''/layout/application.rhtml'' and under some > conditions ( Authenticated user, url parameter) I like to switch > globally to another layout, let''s say custom.rhtml > > Like : > > class AppController < ApplicationController > before_filter :context_layout > > def index > end > end > > # application.rb > def context_layout > if <context_condition> > layout(''custom'') > end > end > > > But layout() call is not available into application.rb, and writing > > class AppController < ApplicationController > > layout(''custom'') > > def index > end > end > > Works,but it''s impossible to add here test conditions like @params. > > The layout method accept a conditions={} parameters, but I dont figure > out how use it. > > Thanks for helping > > Mathieu > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- * J * ~