You want to create a header and footer for every page on your web application. Certain pages should have special headers and footers, and you may want to dynamically determine which header and footer to use for a given request.
Many web applications let you define header and footer files, and automatically include those files at the top and bottom of every page. Rails inverts this pattern. A single file called contains both the header and footer, and the contents of each particular page are inserted into this file.
To apply a layout to every page in your web application, create a file called app/views/layouts/application.rhtml. It should look something like this:
<html> <head> <title>My Website</title> </head> <body> <%= @content_for_layout %> </body> </html>
The key piece of information in any layout file is the directive
<%= content_for_layout %>.
This is replaced by the content of each individual page.
You can make customized layouts for each controller
independently by creating files in the
app/views/layouts folder. For example,
app/views/layouts/status.rhtml is the layout for
StatusController. The layout file for
PriceController would be
Customized layouts override the site-wide layout; they don't add to it.
Just like your main view templates, your layout templates have access to all the instance variables set by the action. Anything you can do in a view, you can do in ...