Clone this wiki locally
There are a few ways to re-use templates:
partial is used to access the representation of another object for use in
object @post node :categories do |post| post.categories.map do |category| partial("categories/show", :object => category) end end
extends is used to inherit from another template:
object @post child :categories do extends "categories/show" end
You can also pass an object to
extends, for example:
node :car do extends "cars/show", object: car end
This can come in handy if you're rendering if you're rendering templates from different contexts and the method cannot be inferred.
When to use Partials and Extends together
partial allows you to reuse templates for multiple objects and
extends allows 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"