backgroundrb: render rhtml templates in worker

Duurde ff voordat ik dat had gevonden maar het equivalent van render_to_string in een backgroundrb process wordt hiermee ondervangen:

Subject: [Backgroundrb-devel] render rhtml templates in worker

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 those.
- 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

Update:‘trailing dash’ was niet zichtbaar ivm html codes, heeft me dus een paar uur gekost :(

Leave a Reply