How switching global layout (application.rthml <or> custom.r


#1

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


#2

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]}”


#3

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


#4

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 C. removed_email_address@domain.invalid wrote:

removed_email_address@domain.invalid
http://lists.rubyonrails.org/mailman/listinfo/rails

  • J *
    ~

#5

Thank you James,

layout :determine_layout # symbol indicates it’s a method call in
this case…

This way rocks,