[Backgroundrb-devel] render rhtml templates in worker
jrobbins at cmj.com
Thu Jan 25 16:45:52 EST 2007
So i have not figured out any sane way to access controllers inside of
backgroundrb or other rails-working scripts but instead i've resorted to
rendering my rhtml templates directly using the ERB module that rails
already includes from ruby standard lib.
There are a few limitations to using ERB in the raw:
- it doesn't understand the trailing dash in <% -%> so you have to strip
- obviously it doesn't know about any rails helper methods although i
think you might be able to explicitly include their classes.
- you have to pass it a binding to your namespace or else it won't be
able to access any instance variables
Here's an example that works from inside of a model class:
#load template into ERB object for rendering
erb = ERB.new( IO.read( rhtml_template_path ) )
#set instance variables for rendering
@report = self
@chart = @report.chart
# in order for the erb template to access the instance variables
# we must pass it a binding to this namespace
b = binding
#render template with instance variables
rendered_result = erb.result(b)
One upside to giving up all the rails framework help is that it runs a
bit faster and lighter.
Jacob Robbins wrote:
> I'm trying to make a worker that will render a large page to a file,
> very similar to what rails' builtin cacheing does except with the
> rendering done outside of rails.
> The only advice I've found is to use
> ActionController::Integration::Session.new but this doesn't seem to
> allow setting session variables the way the functional tests do. Is
> there a way to call a controller's methods or render an rhtml template
> from a background worker?
> Backgroundrb-devel mailing list
> Backgroundrb-devel at rubyforge.org
More information about the Backgroundrb-devel