Add current_layout?

There is no method to get the current layout used in a request. It would be helpful if there was a current_layout similar to controller.controller_name to get the layout that the request is using. Currently the only way to get the current layout is to call a private method: controller.send :_layout, nil, nil, nil

Is there a specific reason why this is not avaiable or do you think this should be added?

This is something I’ve wanted as well. For example, I wanted to change the content security policy based on the layout.

When you’re in the controller, you don’t know what layout you will be using since the render call can specify a layout (even dynamically!). So until you’re actually rendering, you can only answer what the layout might be if you end up using a layout at all and if render doesn’t change it. Even once you’re in the view rendering, layouts can themselves invoke other layouts, so it’s not 100% clear to me what value such a method should return.

1 Like