Skip to content


Subversion checkout URL

You can clone with
Download ZIP


locals should take extra keys more than just object. #297

merged 2 commits into from

2 participants


It seems like Rabl::Renderer only supports :object for the locals. And, I don't understand why :locals even exists if it only supports :object which we can pass it as the first parameter.

With this change, other keys in locals can be used as instance variables in template. And, :object that is passed in as the first parameter will take precedence over the :object in :locals.

Last question, should we even support :object in :locals? It seems like it serves no purpose other than making the code uglier.


:+1: here, but I've also had a couple of beers and everything's looking good right now. I guess I'll let @nesquena verify it unless I get to it tomorrow some time.


Oh, did you look at Pull Request #269 on which is the better option? Just curious.

(my guess is that your test should work in both scenarios)


Yup. Just saw it. I actually tried avoiding modifying engine.rb.

In the mentioned commit, it seems like it is using missing_method, so, instead of using @user, you can just use user. Not sure which is better. But, I would prefer instance variable myself(not messing with method_missing).

@databyte databyte merged commit b22ff37 into nesquena:master
@siong1987 siong1987 deleted the siong1987:locals branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 27, 2012
  1. @siong1987
Commits on Jul 28, 2012
  1. @siong1987
This page is out of date. Refresh to see the latest.
Showing with 23 additions and 2 deletions.
  1. +13 −0
  2. +7 −0 lib/rabl/renderer.rb
  3. +3 −2 test/renderer_test.rb
@@ -440,6 +440,19 @@ Rabl::Renderer.xml(@post, 'posts/show')
These methods allow RABL to be used for arbitrary conversions of an object into a desired format.
+You can also pass in other instance variables to be used in your template as:
+```ruby, 'posts/show', :locals => { :custom_title => "Hello world!" })
+Then, in your template, you can use `@custom_title` as:
+attribute :content
+node(:title) { @custom_title }
### Content Type Headers ###
Currently in RABL, the content-type of your response is not set automatically. This is because RABL is intended
7 lib/rabl/renderer.rb
@@ -31,6 +31,13 @@ def initialize(source, object = nil, options = {})
@options = options
@object = object
+ if @options[:locals]
+ @options[:locals].delete(:object) if @object
+ @options[:locals].each do |k,v|
+ instance_variable_set(:"@#{k}", v)
+ end
+ end
engine.source = self.process_source(source)
5 test/renderer_test.rb
@@ -61,13 +61,14 @@
asserts 'passes :locals to render' do
source = %q{
attribute :name, :as => 'city'
+ node(:zipcode) { @zipcode }
user = => 'irvine')
- renderer =, nil, { :format => 'json', :locals => {:object => user} })
+ renderer =, nil, { :format => 'json', :locals => {:object => user, :zipcode => "92602"} })
- end.equals "{\"user\":{\"city\":\"irvine\"}}".split("").sort
+ end.equals "{\"user\":{\"city\":\"irvine\",\"zipcode\":\"92602\"}}".split("").sort
asserts 'loads source from file' do + "test.json.rabl", "w") do |f|
Something went wrong with that request. Please try again.