Ruby On Rails Classroom image

prateek  darmwal / Professional / Web Technology

 
To post your Question Join Classroom
 
Lesson Topics's No:-  |1 | 2 | 3|Last
Lessons:-Partials

  Partials

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.
app/views/layouts/application.html.erb

              

                               

                                   

 

                  In Listing 5.8, we’ve replaced the HTML shim stylesheet lines with a single call to a Rails helper called render:

< % =   render   'layouts/shim'   %>

 

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.
app/views/layouts/_shim.html.erb

 

<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->

 

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.

 
 
 
image
prateek  darmwal

Skills    Ruby On Rails

Qualifications :- High School - S.K.M. Sn. Sec. School, Haldwani, College/University - Graphic Era Hill University, Bhimtal,
Location :-Dehradun,Dehradun,Uttarakhand,India
Description:- I like to explore new technologies. I have skills in ruby on rails, php5, cakephp, jquery, javascript, html/css, java, c & c++. I love coding
Explore
 

  Students (0)