Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. Working with Layouts
  2.  
  3. Rails uses layouts to interpolate the output of an individual template into a larger whole—a reversal of
  4. the common pattern of including a shared header and footer on every page (which, if you’ve done any
  5. work in languages like PHP and ASP, is all too familiar). The scaffold generator you ran in Chapter 3
  6. created a layout file and placed it in app/views/layouts/application.html.erb. The application.html.erb
  7. layout is applied to all controllers. However, if you like your layout to apply to a specific controller, you
  8. can create a layout file named after the controller you want. For example, a layout that applies only to
  9. the articles controller should be created in app/views/layouts/articles.html.erb. That’s the way it works
  10. 139
  11.  
  12. CHAPTER 6
  13. ACTION PACK: WORKING WITH THE VIEW AND THE CONTROLLER
  14. in Rails. Just as an action tries to render itself using a view that matches its name, a controller attempts
  15. to use a layout that matches its name.
  16. Open the app/views/layouts/application.html.erb in your editor. You should see something like
  17.  
  18. the file shown in Listing 6-6.
  19. Listing 6-6. The app/views/layouts/application.html.erb File
  20. <!DOCTYPE html>
  21. <html>
  22. <head>
  23. <title>Blog</title>
  24. <%= stylesheet_link_tag :all %>
  25. <%= javascript_include_tag :defaults %>
  26. <%= csrf_meta_tag %>
  27. </head>
  28. <body>
  29. <%= yield %>
  30. </body>
  31. </html>
  32. At rendering time, the layout yields the results of the template fragment’s execution in place. See the
  33. <%= yield %> bit that’s highlighted in bold? That’s the important part. Wherever you put the yield
  34. keyword is where your content goes.
  35. One more thing to note: Rails is all about convention over configuration. Here, the convention is
  36. that a layout with the name application.html.erb is automatically applied to all templates unless an
  37. alternate is specified. This means that if you change the name of the layout as it stands, it won’t be
  38. automatically applied. If you want to apply a different layout to a given controller, you can either have a
  39. layout named after the controller or specify it in the controller using the class method layout:
  40. class ExampleController < ApplicationController
  41. layout 'my_layout' # Will use a layout in app/views/layouts/my_layout.html.erb
  42. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement