Rails -- 如何将 HAML 部分的 HTML 输出保存为字符串以发送到外部服务?
我们使用 rake 任务每 24 小时自动生成一份新闻通讯。 新闻通讯顶部有一个部分,管理员可以在其中放置自定义消息。 管理员使用的屏幕有时事通讯的实时预览(他们坚持这一点),使用带有集合的 haml 部分进行渲染。
为了生成和发送电子邮件,我们将 xml 文档发送到第三方 API,其中包含(除其他外)我们要生成的电子邮件的 HTML。
我想要做的是将这个 haml 部分的输出保存在 rake 任务中,类似于 PHP 的 ob_*() 缓冲函数。 有没有办法做类似以下的事情:
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>"
我可能错过了一些明显的东西,我可以想出几种方法来做到这一点,但其中大多数都不是干的,我不想生成大量的html在助手、模型或任务本身中。
让我知道是否有办法可以实现这一目标。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有点盲目,但如果你查看 Haml 的 rdoc,它很可能有一个方法,它接受字符串和一些变量,然后渲染输出。
对于 ERB 来说,它看起来像这样:
A bit of a shot in the dark but if you check out the rdoc for Haml it more than likely has a method which accepts a string and some variables and then renders the output.
With ERB it would look something like:
来自 HAML 文档 的更直接方法
:必须做一些工作来加载 HAML 模板并设置任何需要插值的局部变量,但灵活性可以弥补这一点。
A more direct approach from the HAML docs:
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.
执行此操作的常见方法(通常不建议这样做)是在模型或 rake 任务中渲染为字符串:
请参阅此处以获取更完整的描述:
http://www .compulsivoco.com/2008/10/rendering-rails-partials-in-a-model-or-background-task/
如果您使用页面或片段缓存,则可以从缓存中提取内容。 最简单的方法是启用缓存,然后查看文件的放置位置。
http://guides.rubyonrails.org/caching_with_rails.html
A common way to do this (which is typically advised against) is to render into a string in your model or rake task:
see here for a more full description:
http://www.compulsivoco.com/2008/10/rendering-rails-partials-in-a-model-or-background-task/
If you are using page or fragment caching, you could pull the content out of the cache. The easiest way to do this is to just enable caching, and then look at where the files are being placed.
http://guides.rubyonrails.org/caching_with_rails.html