Ruby On Rails Lesson
Although the layout in Listing 5.1 serves its purpose, it’s getting a little cluttered. The HTML shim takes up three lines and uses weird IE-specific syntax, so it would be nice
to tuck it away somewhere on its own. In addition, the header HTML forms a logical unit, so it should all be packaged up in one place. The way to achieve this in Rails is to use a facility called partials. Let’s first take a look at what the layout looks like after the partials are defined (Listing 5.8).
Listing 5.8 The site layout with partials for the stylesheets and header.
In Listing 5.8, we’ve replaced the HTML shim stylesheet lines with a single call to a Rails helper called render:
Theeffect of this lineis to look for a file called app/views/layouts/_shim.html.erb, evaluateits contents, and insert theresults into the view.6 (Recall that <%= ... %> is the embedded Ruby syntax needed to evaluate a Ruby expression and then insert the results into the template.) Note the leading underscore on the filename _shim.html.erb; this underscore is the universal convention for naming partials and, among other things, makes it possible to identify all the partials in a directory at a glance.
Of course, to get the partial to work, we have to fill it with some content; in the case of the shim partial, this is just the three lines of shim code from Listing 5.1; the result appears in Listing 5.9.
Listing 5.9 A partial for the HTML shim.
<!--[if lt IE 9]>
Similarly, we can move the header material into the partial shown in Listing 5.10 and insert it into the layout with another call to render.