We are generating a newsletter automatically every 24 hours using a rake task. There's a section at the top of the newsletter where an admin can put a customized message. The screen that the admin uses has a live preview of the newsletter (they were insistent on this), rendered using a haml partial that takes a collection.
In order to generate and send the emails, we are sending xml documents to a third party API that contain (among other things) the HTML for the email that we'd like to generate.
What I want to do is save the output of this haml partial within a rake task, something similar to PHP's ob_*() buffering functions. Is there any way to do something like the following:
ob_start();
render :partial => newsletter, :collection => posts
newsletter_html = ob_get_contents()
xml = "
<Creative>
<Name>Our Newsletter -- #{Time.now.strftime('%m/%d/%Y')}</Name>
<HTML><html><body>#{newsletter_html}</body></html></HTML>
...
</Creative>"
I'm probably missing something obvious, and I could think of a few ways to do this but most of them are not DRY and I don't want to be generating a lot of html in the helpers, models, or the task itself.
Let me know if there is a way for me to accomplish this.
A more direct approach from the HAML docs:
require 'haml'
haml_string = "%p Haml-tastic!"
engine = Haml::Engine.new(haml_string)
engine.render #=> "<p>Haml-tastic!</p>\n"
You'll have to do a bit of work loading up the HAML template and setting up any local variables that need interpolation, but the flexibility may make up for that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With