Trouble understanding partials and extend #285

jjaffeux opened this Issue Jul 11, 2012 · 7 comments

4 participants



I'm a new user trying to extend how partials, extends .. and rabl work.

I'm having the following code

node(:notifications) do do |notification| 
    #notification => #<Notification _id: 4ffd44e2f731476e6c00005d, _type: nil, text: "Ohai", type: "notice", start: 2012-07-11 00:00:00 UTC, end: nil, scheduled: false>
    partial("notifications/show", :object => notification)
object @notification
p @notification # is nil

Could you please help me to understand what am doing wrong ?


I think of partials as other templates whereby you supply it with an object. Extend literally copies the contents of the other file into the template.

Here's a better example:

# users/show.rabl
object @presentation

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

# 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"

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

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

Partials allow you to reuse templates (_info.rabl) for multiple objects and extends allow you to reuse templates (public.rabl) from multiple templates.

The distinction is arbitrary. I've mentioned that in a v1 API change, we will combine the two such that passing in an object sets the new context (i.e. how partial works now), otherwise it behaves like extends. The keywords were already there so I don't know why the distinction was made to begin with.

If you look at the source, extends does indeed calls partial with the current object.

Hope that helps!


Thx, great explanation.

You should consider adding this to the README, imo ;)

@jjaffeux jjaffeux closed this Jul 11, 2012

Sure thing! How's that?


great ;)


Thanks @databyte for clarifying and adding a link to that wiki page. Lots of good info has been accumulated on the wiki.


I still do not get it.
As I see it both versions are used to insert output of template into another template. First to usages on ( Partial an Extends ) are producing the same output. Both using categories/show to render collection of categories, but extends somehow guess the object.

But as I found output will not be the same (guessing on my expirence with another model):
partial sample

 "categories": [{"name": "Fruit"},{"name": "Food"}]

extends sample

 "categories": [ {"category: {"name": "Fruit"}},{"category": {"name": "Food"}}]

If you are in a custom "node" and want direct access to the hash for an object, use partial. In other cases, use extends.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment