I wrote the way to build structured layout in Rails.
You can use it when you want to use the same frame according to the page URLs. For example, to show control bar in admin pages.
Environment
I examined in the following environment.
- Rails 3, 4
How to do it?
Usually, app/views/layouts/application.html.erb
is the layout file. This time, let’s create app/views/layouts/sub_application.html.haml
. In my recent project, I created an layout under another directory, such as app/views/something/layouts/application.html.haml
.
1 2 3 4 5 6 7 |
-content_for :content do %header here is the top of content =yield %footer here is the bottom of content =render templates: 'layouts/application' |
According to the Ruby version, you have to write as render :templates =>'layouts/application'
.
Here, defining :content
. And indicate to show :content
in layouts/application
.
1 2 3 4 5 6 7 8 9 |
<!DOCTYPE html> <html> <head> <ttile></title> </head> <body> <%= content_for?(:content) ? yield(:content) : yield %> </body> </html> |
Like this, the layout template shows show :content
if :content
is defined, otherwise it shows yield
.