Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Trouble understanding partials and extend #285

Closed
jjaffeux opened this Issue · 7 comments

4 participants

Joffrey JAFFEUX David Sommers Nathan Esquenazi foton
Joffrey JAFFEUX

Hi,

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

I'm having the following code

#main_view
node(:notifications) do
   @service.notifications.map 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)
  end
end
#partial
object @notification
p @notification # is nil

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

David Sommers
Collaborator

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

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!

Joffrey JAFFEUX

Thx, great explanation.

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

Joffrey JAFFEUX jjaffeux closed this
David Sommers
Collaborator

Sure thing! How's that?

Joffrey JAFFEUX

great ;)

Nathan Esquenazi
Owner

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

foton

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 https://github.com/nesquena/rabl/wiki/Reusing-templates ( 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"}}]
Nathan Esquenazi
Owner

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
Something went wrong with that request. Please try again.