Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

The guy who is in charge of our html emails is leaving and I have been asked to take over. We are an online retailer and send out an email once a week. An email will consist of a main image, a bit of an opening paragraph and then rows of product offers.

As everyone knows html emails are painful to work with due to the archaic html and css you need to deal with. When something needs changing last minute it's a real pain to wade through the spaghetti of td's to get to the link that needs it's url and tracking code changing and the opportunities to make a mistake are plentiful.

So I've decided to create an xml file consisting of all the data that needs to go in the email. For example, the products would be recorded like this:

<products>
    <product id="1">
        <title>My Product</title>
        <image width="160px" height="160px">
            <alt>My Product</alt>
            <url>http://somedomain.com/emails/image1.jpg</url>
        </image>
        <link>
            <term>my_term_for_google_analytics</term>
            <url>http://somedomain.com/products/1</url>
        </link>
    </product>
    <product id="2">
        <title>My Second Product</title>
             ... etc ...
    </product>
</products>

I could then use an xslt template to create the email. The advantage of this is that when a product goes out of stock the morning the email is due to go out I can just change a bit of text in the xml file, generate the html and we're good to go again. I can also use the same xml to create a microsite for the email. The trouble is I've had a good ol' Google about this and I can't find anyone else who has really tried this. So I'm either a visionary genius or an idiot. It usually turns out I'm an idiot so has anyone else had any experience of creating html emails from xml using xslt or can anyone see any major pitfalls with this approach. Is it a good idea?

Disclaimer - I don't really think I'm a potential visionary genius.

share|improve this question
    
Do you have the right tools to deal with html and css? I am certainly somebody who would even advertise their html skills. I think Expression Studio is Microsoft's html5/css editor. Sounds like you are currently doing this stuff in a normal text editor. –  Ramhound Nov 4 '11 at 12:43
    
I do, I'm a web developer primarily so I'm not worried about how to do it, more whether it's worth the trouble (I'm new to xsl). I can see that separating the data from the structure/style should make my life a lot easier in theory. I'm interested to know if anyone has any experience of this in practice, specifically with regards to html emails. –  MrMisterMan Nov 4 '11 at 13:23

5 Answers 5

up vote 5 down vote accepted

We use XSLT templates to transform XML into HTML for reports all the time. It works well, but like anything else you can take it too far. Keep it simple or you end up with some pretty out of hand XSLT that nobody can understand.

share|improve this answer
1  
Thanks, that's good advice. The trouble is html emails are very difficult to keep simple! –  MrMisterMan Nov 4 '11 at 13:19
    
@MrMisterMan " An email will consist of a main image, a bit of an opening paragraph and then rows of product offers." doesn't sound that complicated –  Gratzy Nov 4 '11 at 13:26
    
On the web, no problem. In a html email template (that I didn't design) I have nested tables within nested tables within nested tables, spacer gifs, font tags etc. It's horrible. When I get time I intend to redesign it but for now I want to have to deal with that code as little as possible. –  MrMisterMan Nov 4 '11 at 13:38
1  
@MrMisterMan I was joking knowing that you were simplifying your situation, I can fully appreciate the pain in inheriting old html templates, I feel for you. –  Gratzy Nov 4 '11 at 13:44
    
Ha! Thanks, I appreciate your empathy. –  MrMisterMan Nov 4 '11 at 13:45

Transforming XML into HTML is fairly standard practice. So yes it’s a good idea but no you’re not a genius. :)

share|improve this answer
    
Ha! Yes I accept I'm not a genius. I'm just surprised I can't find anyone doing it for html emails. I was worried it may be because the poor html you need to deal with when creating html emails makes the whole process more trouble than it's worth. –  MrMisterMan Nov 4 '11 at 11:11

Having an intermediate XML format will serve you right. Firstly, because with XSLT you will be able to cleanly recreate an HTML email, and secondly, because you will have your clean XML content ready if any other application needs it. Also, XSLT is just great for that job.

Just one remark about your "microsite".

  1. Remember that just about every email application now blocks JavaScript (and ActiveX for that matter) from working, as a security measure. So No Javascript. I've tried doing it until I finally googled it up and realized this would never work.
  2. Use inline CSS, and by the way remember that some of your clients may have older versions of Internet Explorer (used by Outlook to render them), and as such, your email may not look like it should be with modern browsers. I do have Outlook 2003 for example.
  3. There are other things to know of which you can see here, but the most important one to me is to make sure to propose a plain text alternative version. Since you will be using XSLT, you could make a good choice by writing another script for transforming into plain text.
share|improve this answer
    
ActiveX thats funny :) –  Gratzy Nov 4 '11 at 13:29
    
Thanks Jalayn. I'm aware of points 1 and 2 but point 3 is a very good idea, I hadn't thought of creating the plain text version with xsl. –  MrMisterMan Nov 4 '11 at 13:42
    
@Gratzy Thankfully, I've never had to work with that :-) –  Jalayn Nov 4 '11 at 13:44

It's a good idea to have an intermediate XML format, but using XSLT to generate HTML files is so 1999.. it might be a better option to use a templating engine such as Velocity, Spark View Engine, Razor, StringTemplate etc depending on what technology you're using. I'm sure you'll find them much easier to work with than the monstrous beast that is XSLT...

share|improve this answer
    
Thanks Matt, I'll look into those... –  MrMisterMan Nov 4 '11 at 13:41
1  
I would be interested if you know of any objective comparisons of these technologies, rather than "it's so 1999" which sounds more like a fashion statement than a technical evaluation. One key advantage of XSLT is that it's a standard supported by multiple tools. It's also worth bearing in mind that in this kind of application there's no reason not to use XSLT 2.0 ("so 2007...") –  Michael Kay Nov 4 '11 at 14:27
    
as you wish: perlmonks.org/?node_id=142061 –  MattDavey Nov 4 '11 at 14:42
    
I understand there are many people out there who are fond of XSLT and as a technology it certainly has its place, however personally I would say XSLT is a little too generalized for generating HTML pages - I apologise if this came accross as XSLT bashing :) –  MattDavey Nov 4 '11 at 16:44

In your case, XSLT is acting as just another template engine.

So, is it a good idea to use a template engine in your case?

Completely! You already highlighted several advantages in your question: being able to modify the content without having to deal with HTML and vice versa, or being able to use the same content both for e-mails and on the website (even if, performance wise, you may consider using a real database for the website, from which to produce XML through serialization).

Now, is it a good idea to use XSLT for your templates?

It depends. Some developers find it 1999y; others hate it so much that they would use no matter what as far as it is not XSLT; others know XSLT quite well, enjoy it and use it frequently.

It also depends on the other technologies used. If your website is in PHP, why not using Smarty or other template engines which would allow you to write a more lightweight code compared to XSLT? If you use .NET Framework, you may want to consider Razor, a template engine used in ASP.NET MVC websites.

share|improve this answer
    
An excellent answer, thank you. –  MrMisterMan Nov 4 '11 at 15:57
    
+1 XSLT is a very powerful generalized technology with many uses. An HTML template engine is a little more specialised towards this particular scenario. –  MattDavey Nov 7 '11 at 9:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.