Rails3 get current layout name inside view

Viewed 9315

I have the answer for the Rails 2.X but not for Rails 3. How can I read the name of a current layout rendered inside a view.

My Rails2 question: Rails Layout name inside view

Thx.

8 Answers

For rails 5:

controller.class.send(:_layout)

This does NOT work:

controller.send(:_layout)

All the approaches in the previous answers try to guess the name via private methods, but there's no need to guess and can be easily accomplished with the public API:

class ApplicationController
  layout :set_layout
  attr_reader :layout_name
  helper_method :layout_name

  private

  def set_layout
    @layout_name = "application"
  end
end

Override in any controller that won't use the standard layout:

class MyController < ApplicationController
  private

  def set_layout
    @layout_name = "my_layout"
  end
end

And now in your views:

<%= layout_name %>
Related