Rails’ layout mechanism follows the standard pattern embodied in Sun’s J2EE patterns and familiar to users of Struts and other frameworks. The page template outputs the main content, and it’s automatically wrapped by a generic template (as opposed to the earlier SSI-style paradigm, where each page manually includes headers, footers, etc.). There’s always a complication with this pattern, which is that the page author usually wants to influence some aspects of the wrapping as well.
For instance, you’re writing books.rhtml for an E-Commerce site. You’re obviously going to set the main content - a list of books - but you also want to influence the wrapping, such as: change the page title (in the doc head), highlight “books” in the side menu, etc.
With many frameworks, you have to jump in to an XML file to influence the wrapping, which often means no-one bothers, and usability - not to mention search engine optimisation - suffers as a result…all pages end up with the same title and meta-information, and you’re probably downloading extra Javascript because you’ve made it the same for all pages.
Rails makes it quite easy, and being Rails, there’s no funny config files to mess with…it’s all code. The layout template is parsed after the main content, so the main content can set any variables, and the layout template can make use of them.
For example, standard-layout.rhtml says: [html]