简体   繁体   English

Rails布局名称在视图中

[英]Rails Layout name inside view

How can I print out the name of current layout in a view? 如何在视图中打印出当前布局的名称?

Example

puts controller.current_layout

Thx 谢谢

This works with Rails 3.0.7: 这适用于Rails 3.0.7:

controller.send :_layout

Obviously, it's a private method, so use at your own risk. 显然,这是一种私有方法,因此使用风险自负。

This works for me: response.layout 这对我有用:response.layout

UPDATE: True, response.layout does not work in rails3+ thus I usually define a @layout_name variable inside each layout. UPDATE:是的,response.layout在rails3 +中不起作用,因此我通常在每个布局中定义一个@layout_name变量。

Example of application.html.haml application.html.haml的示例

- @layout_name = 'application'
!!! Strict
%html
...

<%= controller.active_layout %> gets you the layout file name. <%= controller.active_layout %>获取布局文件名。 So layout 'application' would return layouts/application.html.erb 因此layout 'application'将返回layouts/application.html.erb

In Rails 2.1.0 and greater . 在Rails 2.1.0和更高版本中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM