Skip to content
nesquena edited this page Sep 23, 2012 · 9 revisions

There are a few ways to re-use templates:

Partial

Partial is used to access the representation of another object for use in node:

object @post

node :categories do |post|
  post.categories.map do |category| 
    partial("categories/show", :object => category) 
  end
end

Extends

Extends is used to inherit from another template:

object @post

child :categories do
  extends "categories/show"
end

When to use Partials and Extends together

Partials allow you to reuse templates for multiple objects and extends allow you to reuse templates from multiple templates.

# users/show.rabl
object @presentation

node :blogger do
  partial "writers/info", object: presentation.blogger
end
node :author do
  partial "writers/info", object: presentation.author
end

# writers/_info.rabl
object @writer  # ignored when used as a partial so I usually don't specify it
attributes :name, :first_published_date, :birth_date, :death_date
child :publications do
  extends "publications/public"
end

# publications/public.rabl
attributes :title, :publication_date, :publisher

# publications/show.rabl
object @publication
extends "publications/public"