diff --git a/README.md b/README.md index 1d4bb2e..8b6842a 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,14 @@ end template.render ``` +### Hanami + +Follow these steps: + + 1. Add `tilt-jbuilder` gem to your project. + 1. Generate a template (eg: `apps/api/templates/events/index.json.jbuilder`) + 1. Profit + ## Contributing 1. Fork it 2. Create your feature branch (`git checkout -b my-new-feature`) diff --git a/lib/tilt/jbuilder.rb b/lib/tilt/jbuilder.rb index 3067662..8a1014c 100644 --- a/lib/tilt/jbuilder.rb +++ b/lib/tilt/jbuilder.rb @@ -88,9 +88,9 @@ def initialize_engine def prepare; end def evaluate(scope, locals, &block) - scope ||= Object.new + scope ||= BasicObject.new ::Tilt::Jbuilder.encode(scope) do |json| - context = scope.instance_eval { binding } + context = scope.instance_eval { ::Kernel.binding } set_locals(locals, scope, context) if data.kind_of?(Proc) return data.call(::Tilt::Jbuilder.new(scope)) @@ -103,9 +103,10 @@ def evaluate(scope, locals, &block) private def set_locals(locals, scope, context) view_path = options[:view_path] - scope.send(:instance_variable_set, '@_jbuilder_view_path', view_path) - scope.send(:instance_variable_set, '@_jbuilder_locals', locals) - scope.send(:instance_variable_set, '@_tilt_data', data) + scope.instance_exec(binding) { + @_jbuilder_view_path = view_path + @_jbuilder_locals = locals + } set_locals = locals.keys.map { |k| "#{k} = @_jbuilder_locals[#{k.inspect}]" }.join("\n") eval set_locals, context end diff --git a/spec/tilt-jbuilder_spec.rb b/spec/tilt-jbuilder_spec.rb index 3924093..5353c13 100644 --- a/spec/tilt-jbuilder_spec.rb +++ b/spec/tilt-jbuilder_spec.rb @@ -29,6 +29,11 @@ "{\"author\":\"Anthony\"}".should == template.render(Object.new, :name => 'Anthony') end + it "should pass basic object locals" do + template = Tilt::JbuilderTemplate.new { "json.author name" } + "{\"author\":\"Anthony\"}".should == template.render(BasicObject.new, :name => 'Anthony') + end + it "should evaluate in an object scope" do template = Tilt::JbuilderTemplate.new { "json.author @name" } scope = Object.new