开发者

Rails3 get current layout name inside view

I have the answer for the Rails 2.X but not for Rails 3. How can I read the name of a curren开发者_C百科t layout rendered inside a view.

My Rails2 question: Rails Layout name inside view

Thx.


Getting this to work in Rails 3.2 is a little more complicated than previously outlined. If your controller explicitly declares a layout, then the result of controller.send(:_layout) is a String, but otherwise it's an ActionView::Template. Try this:

module ApplicationHelper
  def current_layout
    layout = controller.send(:_layout)
    if layout.instance_of? String
      layout
    else
      File.basename(layout.identifier).split('.').first
    end
  end
end


in rails 5

This works for me:

def current_layout
  layout = controller.class.send(:_layout)
  if layout.nil?
    default_layout
  elsif layout.instance_of? String or layout.instance_of? Symbol
    layout
  else
    File.basename(layout.identifier).split('.').first
  end
end


For Rails 4:

controller.send(:_layout)
=> 'application'     

For Rails 3.2:

controller.send(:_layout)
=> #<ActionView::Template:0x000000082bb788> 

But controller.send(:_layout).identifier returns the fullpath:

/home/davidm/Documentos/Devel/myapp/app/views/layouts/application.haml


I think it should be in core, but for now you can make a helper method:

 def current_layout
    controller.send :_layout
  end

it will return currently used layout name


I have used in Rails4 at view pages and got reuslt.

controller.send(:_layout)

I hope this help.

Rails3 get current layout name inside view


For rails 5:

controller.class.send(:_layout)

This does NOT work:

controller.send(:_layout)


You can do what I've done in my Ajax gem for Rails which is to wrap the _render_layout method:

    ActionView::Base.class_eval do
      def _render_layout_with_tracking(layout, locals, &block)
        controller.instance_variable_set(:@_rendered_layout, layout)
        _render_layout_without_tracking(layout, locals, &block)
      end
      alias_method_chain :_render_layout, :tracking
    end

Then you can access the value that was set from your view (I'm pretty sure you have access to the controller there...) or in your controller in an after_filter, which is what I do.

I've written a custom RSpec 2 matcher which can be used to test layout rendering in Rails 3.


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 %>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜