Skip to content
This repository
Browse code

First draft of the ActionView Partials guide.

  • Loading branch information...
commit 8a090aa93dcd707c915fe0010e5d4446d1690c97 1 parent 377b2fd
Nick Laiacona authored

Showing 1 changed file with 91 additions and 0 deletions. Show diff stats Hide diff stats

  1. +91 0 railties/doc/guides/actionview/partials.markdown
91 railties/doc/guides/actionview/partials.markdown
Source Rendered
... ... @@ -0,0 +1,91 @@
  1 +A Guide to Using Partials
  2 +===============================
  3 +
  4 +This guide elaborates on the use and function of partials in Ruby on Rails. As your Rails application grows, your view templates can start to contain a lot of duplicate view code. To manage and reduce this complexity, you can by abstract view template code into partials. Partials are reusable snippets of eRB template code stored in separate files with an underscore ('_') prefix.
  5 +
  6 +Partials can be located anywhere in the `app/views` directory. File extensions for partials work just like other template files, they bear an extension that denotes what kind of code they generate. For example, `_animal.html.erb` and `_animal.xml.erb` are valid filenames for partials.
  7 +
  8 +Partials can be inserted in eRB template code by calling the `render` method with the `:partial` option. For example:
  9 +
  10 + <%= render :partial => 'foo' %>
  11 +
  12 +This inserts the result of evaluating the template `_foo.html.erb` into the parent template file at this location. Note that `render` assumes that the partial will be in the same directory as the calling parent template and have the same file extension. Partials can be located anywhere within the `app/views` directory. To use a partial located in a different directory then it the parent, add a '/' before it:
  13 +
  14 + <%= render :partial => '/common/foo' %>
  15 +
  16 +Loads the partial file from the `app/views/common/_foo.html.erb` directory.
  17 +
  18 +Abstracting views into partials can be approached in a number of different ways, depending on the situation. Sometimes, the code that you are abstracting is a specialized view of an object or a collection of objects. Other times, you can look at partials as a reusable subroutine. We'll explore each of these approaches and when to use them as well as the syntax for calling them.
  19 +
  20 +Partials as a View Subroutine
  21 +-----------------------------
  22 +
  23 +Using the `:locals` option, you can pass a hash of values which will be treated as local variables within the partial template.
  24 +
  25 + <%= render :partial => "person", :locals => { :name => "david" } %>
  26 +
  27 +The variable `name` contains the value `"david"` within the `_person.html.erb` template. Passing variables in this way allows you to create modular, reusable template files. Note that if you want to make local variables that are optional in some use cases, you will have to set them to a sentinel value such as `nil` when they have not been passed. So, in the example above, if the `name` variable is optional in some use cases, you must set:
  28 +
  29 + <% name ||= nil -%>
  30 +
  31 +So that you can later check:
  32 +
  33 + <% if name -%>
  34 + <p>Hello, <%= name %>!</p>
  35 + <% end -%>
  36 +
  37 +Otherwise, the if statement will throw an error at runtime.
  38 +
  39 +Another thing to be aware of is that instance variables that are visible to the parent view template are visible to the partial. So you might be tempted to do this:
  40 +
  41 + <%= render :partial => "person" %>
  42 +
  43 +And then within the partial:
  44 +
  45 + <% if @name -%>
  46 + <p>Hello, <%= @name %>!</p>
  47 + <% end -%>
  48 +
  49 +The potential snag here is that if multiple templates start to rely on this partial, you will need to maintain an instance variable with the same name across all of these templates and controllers. This approach can quickly become brittle if overused.
  50 +
  51 +Partials as a View of an Object
  52 +--------------------------------
  53 +
  54 +Another way to look at partials is to view them as mini-views of a particular object or instance variable. Use the `:object` option to pass an object assigns that object to an instance variable named after the partial itself. For example:
  55 +
  56 + # Renders the partial, making @new_person available through
  57 + # the local variable 'person'
  58 + render :partial => "person", :object => @new_person
  59 +
  60 +If the instance variable `name` in the parent template matches the name of the partial, you can use a shortcut:
  61 +
  62 + render :partial => "person"
  63 +
  64 +Now the value that was in `@person` in the parent template is accessible as `person` in the partial.
  65 +
  66 +Partials as a View of a Collection
  67 +-----------------------------------
  68 +
  69 +Often it is the case that you need to display not just a single object, but a collection of objects. Rather than having to constantly nest the same partial within the same iterator code, Rails provides a syntactical shortcut using the `:collection` option:
  70 +
  71 + # Renders a collection of the same partial by making each element
  72 + # of @winners available through the local variable "person" as it
  73 + # builds the complete response.
  74 + render :partial => "person", :collection => @winners
  75 +
  76 +This calls the `_person.html.erb` partial for each person in the `@winners` collection. This also creates a local variable within the partial called `partial_counter` which contains the index of the current value. So for example:
  77 +
  78 + <%= partial_counter %>) <%= person -%>
  79 +
  80 +Where `@winners` contains three people, produces the following output:
  81 +
  82 + 1) Bill
  83 + 2) Jeff
  84 + 3) Nick
  85 +
  86 +One last detail, you can place an arbitrary snippet of code in between the objects using the `:spacer_template` option.
  87 +
  88 + # Renders the same collection of partials, but also renders the
  89 + # person_divider partial between each person partial.
  90 + render :partial => "person", :collection => @winners, :spacer_template => "person_divider"
  91 +

0 comments on commit 8a090aa

Please sign in to comment.
Something went wrong with that request. Please try again.