When dealing with rendering data for an email, one frequently has to make many database calls to assemble the required data. This can be slow, and depending on how you structure the code that is assembling the data vs rendering the data in a template, it’s very easy to be making repeated calls, which can significantly slow down your process. Additionally, whether you are using Haml, Mustache, Jade, or any other templating language, embedding too much logic in the template can making things hard to maintain (especially if some logic lives in the template and some elsewhere in your domain code). Of course some logic in the template (a conditional: should I render this section?, or loops: render this hash of data) is necessary, but I like to keep as much out of there as possible. It’s easier to optimize, debug and maintain that logic elsewhere, and also writing complex logic in Ruby is much more fun than in a templating language!
In this article I’ll present what I’ve been doing to keep my templates relatively logic-free, and how I make sure I don’t repeat any heavy database calls in assembling my data.





