More on Templates


(Notes from Chapter 23 of “Agile Web Development with Rails, Third Edition”, section 23.9)

Layouts: allow for rendering pages nested within other pages, i.e. for the standard page layout across all actions

<%= yield :layout %> in the layout file will yield to the action’s template(s)

  • default layout name is the name of the controller, i.e. the class MainController would correspond to the layout “main”
  • since all controllers extend from ApplicationController, the default standard layout is “application”
  • these files are found in the app/views/layouts directory
  • you can override this by writing layout “standard” inside the controller
    • with this, you can use :except and :only, which can be arrays of strings or just a single string
    • a parameter of nil would turn off layouts for that controller
    • a symbol parameter, like :determine_layout, would call that instance method to determine which layout to use – this method must return a string name of a layout
  • controller subclasses will use the parent’s layout unless they override it by having their own layout file or re-calling layout themselves
  • actions can override the layout as well, using the :layout option in render
  • layouts have access to everything that normal templates have access to
  • regular (action’s) template is rendered before the layout is called, so the template could set a @title variable which would be then accessible to the layout to use
    • yield can also be used to render other types of content than just the main page content, such as a sidebar or additional JavaScript functions:
      <% content_for(:sidebar) do %>

      <% end %>
    • this content will then be stored internally until yield :sidebar is encountered and will not be rendered in the normal action template

Partials: a subroutine displaying a specific, re-usable template; also known as “partial-page templates

  • the template filename must start with an underscore
  • use <%= render :partial => “row” %>
    • by default, the object passed to this has the name of the view’s corresponding model, i.e. @user, but you can override which the instance variable is with the :object option on render
    • partials have local variables named after the model, such as user
    • can also pass in :locals, a hash of additional local variables
  • This is really cool! We can use partials to render something for each element in a collection!
    • pass the variable as the :collection parameter on render
    • you can still use the local variable user like before
    • the parameter :spacer_template is optional and allows for a template that will be rendered between each of the collection’s elements
  • if the partial template in render contains at least one slash character, then Rails assumes that it is a path from the  app/views directory instead of within the current controller’s views
  • convention for “shared” templates is to store them in a directory called shared off of app/views
  • controllers and templates can both render partials (useful for updating part of the page with Ajax)

No Responses Yet to “More on Templates”

  1. Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: