nesquena / rabl

General ruby templating with json, bson, xml, plist and msgpack support
http://blog.codepath.com/2011/06/27/building-a-platform-api-on-rails/
MIT License
3.65k stars 335 forks source link

Render RABL outside Controller #86

Closed nesquena closed 13 years ago

nesquena commented 13 years ago

Talking with @svenfuchs and reminded me that it might be helpful to render RABL outside of a controller context (for messaging queues, etc). One simple possibility is to support partial at the class level. We already support:

partial("path/to/file", :object => @test)

Perhaps the simplest thing to do is to extend this as a class level method:

Rabl::Engine.render("path/to/file", :object => @test)

or something similar. Thoughts?

nesquena commented 13 years ago

Another way is to use the existing system:

source = File.read('templates/really_cool_template.rabl')
rabl_engine = Rabl::Engine.new(source, :format => 'json')
output = rabl_engine.render(self, {})
puts output

That would work today.

svenfuchs commented 13 years ago

Awesome, thanks

I think we need a mini-controller-type place to wire up views with models anyway because I don't want this to be part of the model.

I'm looking into this today.

Thanks!

On Aug 6, 2011, at 4:46 AM, nesquena wrote:

Another way is to use the existing system:

source = File.read('templates/really_cool_template.haml')
rabl_engine = Rabl::Engine.new(source, :format => 'json')
output = rabl_engine.render(self, {})
puts output

That would work today.

Reply to this email directly or view it on GitHub: https://github.com/nesquena/rabl/issues/86#issuecomment-1740489

nesquena commented 13 years ago

Yeah I agree, good idea to separate out these views from the model as much as possible IMO, its just clutter not business logic. Interested to see what you guys come up with. I will probably create that helper for rendering as defined above:

Rabl::Engine.render("path/to/file", :object => @test, :format => 'json')

Unless you have a better syntax suggestion.

svenfuchs commented 13 years ago

Hmm, actually so far I think what rabl has is good enough, except that I'd maybe want to use caching of path lookups and template files, like actionview does that (and tilt, too, i guess?)

This is what I have now, and it works fine for now:

https://gist.github.com/bd5415e6e12b1425d271

Cheers!

On Aug 7, 2011, at 12:40 AM, nesquena wrote:

Yeah I agree, good idea to separate out these views from the model as much as possible IMO, its just clutter not business logic. Interested to see what you guys come up with. I will probably create that helper for rendering as defined above:

Rabl::Engine.render("path/to/file", :object => @test, :format => 'json')

Unless you have a better syntax suggestion.

Reply to this email directly or view it on GitHub: https://github.com/nesquena/rabl/issues/86#issuecomment-1746666

nesquena commented 13 years ago

Oh, excellent. That's a good approach. In that case, perhaps this is closed without me actually committing a line of code. Do let me know if you need any other rabl-related support / fixes though through this process.