问题
How can I print out the name of current layout in a view?
Example
puts controller.current_layout
Thx
回答1:
This works with Rails 3.0.7:
controller.send :_layout
Obviously, it's a private method, so use at your own risk.
回答2:
This works for me: response.layout
UPDATE: True, response.layout does not work in rails3+ thus I usually define a @layout_name variable inside each layout.
Example of application.html.haml
- @layout_name = 'application'
!!! Strict
%html
...
回答3:
<%= controller.active_layout %>
gets you the layout file name. So layout 'application'
would return layouts/application.html.erb
In Rails 2.1.0 and greater.
来源:https://stackoverflow.com/questions/2622219/rails-layout-name-inside-view