I found myself needing to render a .rxml template from my Model today (Ok, stop the MVC preach already...) anyways.. found a few deadends, but finally got down to use method_missing to see what's needed by ActionViewer::Base. Not sure if there's an easier way...
#
# lib/render_anywhere.rb
#
# Render templates outside of a controller or view.
#
# Simply mixin this module to your existing class, for example:
#
# class MyTemplater < ActiveRecord::Base
# include RenderAnywhere
#
# And you can use render() method that works the same as ActionView::Base#render
#
# obj = MyTemplater.new
# obj.html = obj.render :file => '/shared/header'
#
#
module RenderAnywhere
class DummyController
def logger
RAILS_DEFAULT_LOGGER
end
def headers
{}
end
end
def render(options, assigns = {})
viewer = ActionView::Base.new(Rails::Configuration.new.view_path, assigns, DummyController.new)
viewer.render options
end
def template_exists?(path, assigns = {})
viewer = ActionView::Base.new(Rails::Configuration.new.view_path, assigns, DummyController.new)
viewer.pick_template_extension(path) rescue false
end
end
Hope this is helpful to someone
Great, but... How to pass variables to the template?? I've tried with :locals=>{:somevar=>'value'} and doesn't works.
Thanks
Posted by: Matias | August 07, 2006 at 05:48 AM
Yea, I haven't figured that out yet. maybe when I need it - my problem that day was to render a rxml file with variables from elsewhere.
Sure would love to hear any solution. Can probably tweak DummyController, but I'm not very sure.
Posted by: choonkeat | August 07, 2006 at 10:02 AM
You can actually render a template with a one liner, .eg:
ActionView::Base.new(Rails::Configuration.new.view_path).render(:partial => 'foo/bar', :locals => {:foo => 'bar'})
Posted by: willc | November 12, 2007 at 11:37 PM
@willc: much better! thanks :-)
Posted by: choonkeat | November 13, 2007 at 08:09 AM
This has come up often for me and I wanted a more elegant solution. So I put together ActsAsRenderer to give models a simple and elegant way to render output to strings or files.
Please see:
http://davetroy.blogspot.com/2008/02/actsasrenderer-brings-output-to-models.html
Posted by: Dave Troy | February 06, 2008 at 05:41 AM
Setting instance variables for the templates is easy: just add the name and value to the ActionView instance's @assigns hash, like this:
av = ActionView::Base.new(Rails::Configuration.new.view_path)
av.assigns[:foo] = "bar"
av.render "some/template/here"
Posted by: Adam | June 06, 2008 at 03:32 PM
Thanks for sharing this. I've got it mostly working but need to use UrlHelpers.
In other words, I'd like to include things like this in my templates (bad example):
Suggestions on how to do this?
Thanks
Jeff
Posted by: Jeff | August 18, 2008 at 07:56 AM
Er, looks like it stripped out the rhtml which is the following wrapped correctly:
link_to 'Edit', edit_user_path(@user)
Posted by: Jeff | August 18, 2008 at 07:58 AM
You could use:
result = ERB.new(template_content).result(binding)
Olivier
Posted by: Olivier | November 17, 2008 at 06:16 PM
Adam's suggestion worked for me. The RenderAnywhere did not! Rails 2.3.3.
Posted by: eric | December 03, 2009 at 04:54 AM