I’m a big fan of Jon Bentley, the author of Programming Pearls. In this book, he discusses elegant solutions to problems that have irritated programmers for years. (I was in Europe for 6 weeks last year, and this was the one book that remained consistently in my bag throughout the whole trip. I’m not joking.)
In chapter 3 of the second edition, Bentley demonstrates how “form letters” (another name for templates) can be used to solve some particularly tedious programming problems.
Like most web frameworks, Django (everyone’s favorite web stack) ships with its own template engine. In pretty much any Django tutorial, you’ll find a section on how to use templates to implement the dynamic pages on your website. This can lead to a sort of tunnel-vision that causes you to forget that templates exist outside of the world of dynamically generated HTML.
However, as Bentley was so kind to remind me, templates are good for so much more than generating HTML pages to be served over HTTP. I often find myself attacking ugly problems with a silly custom solution until I have that aha! moment in which I realize that templates are the perfect answer.
A couple of examples:
Email templates:
I do contract web development here and there. About a year ago, I was working on a client site that sent automated plain-text emails to users in response to certain events. The site owner wanted an easy way to edit these emails. I won’t tell you how silly my first solution to this problem was.
Luckily, I was quick to realize that plain text can be templated just as easily as HTML can. So, I made a template directory specifically for these emails, and documented the variables that would be passed into each template in comments at the top of each relevant file. The code to send the emails involves querying the relevant information from the database, producing a dictionary of key-value pairs from this data, and feeding this into the appropriate template to produce the email text.
The best part was that when the site owner decided that he also wanted to maintain HTML versions of his newsletters, I was able to use template inheritance to keep all of the text for both plaintext and HTML versions of each newsletter in one place.
XML API responses:
My masters thesis was in data management. If you had walked by my desk on any given day when I was working on my thesis project, you probably would have heard me muttering “I hate XML” as I stared maniacally into my editor window.
The StopFinder XML API was one of the last features to be implemented before our 1.0 release. I was starting to get a wee bit tired. Coffee was becoming more of a dependency than a performance enhancer. You know how it is. The last thing I wanted to do was mess around with code to build XML responses. Parsing it is bad enough.
After about five minutes of reading about different Python modules that can build XML documents programmatically, I had the predictable aha! moment. Why not just use a template to specify how the response should look? My favorite thing about using templates to implement XML responses is that the document structure is very obvious to maintenance programmers, in comparison to some nasty module that uses the DOM to build a document in code.
One word of warning with this approach: The natural way to implement an XML API from here is to write a vanilla view function to accept POST requests, or something like this. However, if this view function is in the same app as the rest of your site’s functionality, an out-of-control API consumer could potentially bog down the performance of your entire site. A better thing to do would probably be to run a seperate Django instance specifically for your API. Of course, if the API hits a shared database really hard, you’re probably going to be in trouble no matter what. But that’s a story for another article.
So, the lesson of the day is to remember that templates are a general purpose tool. This is probably obvious to most clever folks, but the slow ones like me occasionally need to be reminded.
May 1st, 2008 at 6:16 am
I recommend reading http://hsivonen.iki.fi/producing-xml/ before producing any XML. A template isn’t such a good idea.
May 1st, 2008 at 10:52 am
That’s a great article, thanks for the link.
In the case of complex output, I agree that the universe of errors you can make with a template is quite large. However, I think it’s a good solution for something on the scale of the StopFinder API. I wouldn’t use it to generate my own RSS feed, for example — I’d have to match the output to an existing spec, which is hard enough with stringent validation in place.
(P.S. From your website, I see you’re interested in Factor too — I’m excited to see how that language evolves!)
Thanks for the comment.
October 28th, 2008 at 6:03 pm
This is great info to know.
April 9th, 2009 at 2:30 pm
Perhaps using the django template enjine, combined with a templatetag that automatically escapes things properly, would be a really nice way to do things.
The linked article is a little too purist. It says, for example:
“…Text-based Web templating systems (MovableType, WordPress, etc.) and active page technologies that seem to allow you to embed program code in document skeleton (ASP, PHP, JSP, Lasso, Net.Data, etc.) are designed for tag soup. They don’t guarantee well-formed XML output. They don’t guarantee correct HTML output, either. They seem to work with HTML, because text/html user agents are lenient …”
He doesn’t even think template systems are good for writing HTML? Whatever.
For a lot of applications, I’d rather have the clarity and maintainability of the template engine over the warm feeling I get from having everything pass muster with wacko purists.
There’s a time and a place for ultra purity. But there’s a time and a place for GTTFD, too.
August 30th, 2009 at 5:43 pm
Great blog you got here…keep up the good work.
September 10th, 2009 at 5:46 pm
Hi! I was surfing and found your blog post… nice! I love your blog.
Cheers! Sandra. R.
October 22nd, 2009 at 11:29 am
Sign: zbdtg Hello!!! setqy and 2758mxpsdqszqd and 9431 : Hi! I was surfing and found your blog post! nice! I just came across your blog and wanted to say that Ive really enjoyed it.
October 24th, 2009 at 8:27 am
Sign: tpmnp Hello!!! kmgwa and 7419sqefzxqzmw and 1149 : I love your site.
Love design!!! I just came across your blog and wanted to say that Ive really enjoyed browsing your blog posts.
March 6th, 2010 at 2:46 pm
Thanks for this blog. I am new at development and this will help a lot.
October 6th, 2010 at 6:30 am
I can’t say I agree with everything you’ve said here, but at least you wrote it well, unlike lots of crappy bloggers out there!
October 30th, 2010 at 1:08 am
You you should edit the page name Django Templates: Good for so much more than HTML - PitStop to something more catching for your blog post you write. I enjoyed the post however.
December 17th, 2010 at 10:16 am
Interesting read. I wish I felt inspired enough to write such good posts onto my own blog. It is not easy.
January 14th, 2011 at 1:35 pm
This is good story I love so much Thanks.
April 1st, 2011 at 12:16 am
It is real happiness to study you blog!
May 31st, 2011 at 4:34 pm
You lost me, buddy. I mean, I suppose I get what youre saying. I get where youre coming from. But you just seem to have forgotten that you’ll find folks out there who can see this issue for what it genuinely is and could possibly not agree with you. You seem to alienate a whole bunch of individuals who may have been fans of your web site.
October 7th, 2011 at 8:48 pm
Appreciating the determination you add in to your site and detailed information and facts you deliver. It’s wonderful to find a weblog once in a while that isn’t the same kind of rehashed info. Fantastic read! I’ve book marked your site and even integrating your RSS feeds to my own Google account.
November 3rd, 2011 at 1:58 pm
Hey there! Someone in my Facebook group shared this site with us so I came to check it out. I’m definitely loving the information. I’m book-marking and will be tweeting this to my followers! Wonderful blog and brilliant style and design.
November 26th, 2011 at 3:58 pm
from bad chemical substances. The toxins are eliminated and the physique and deal with
November 30th, 2011 at 10:01 pm
I’ve the exact same challenge! I’m nevertheless operating difficult now! Hop this helps!
December 2nd, 2011 at 5:44 pm
Your site has really inspired me to rethink the way I run my site. Just saying thanks for your great work.
December 3rd, 2011 at 4:11 pm
I haven’t checked in here for some time as I thought it was obtaining boring, but the last couple of posts are excellent quality so I guess I’ll add you back to my everyday bloglist. You deserve it my friend
December 5th, 2011 at 2:45 pm
Useful site. I may well have applied a lot of of the info and not have burned so much energy.
January 5th, 2012 at 2:03 am
{considered to be|regarded as|regarded as being|thought to be|widely
January 20th, 2012 at 3:19 am
The following time I read a weblog, I hope that it doesnt disappoint me as much as this one. I imply, I know it was my option to learn, but I truly thought youd have one thing interesting to say. All I hear is a bunch of whining about one thing that you would fix in the event you werent too busy on the lookout for attention.
January 22nd, 2012 at 3:50 pm
Hi there! This post couldn’t be written any better! Reading through this post reminds me of my previous room mate! He always kept talking about this. I will frward this article to him. Pretty sure he will have a good read. Thank you for sharing!
January 31st, 2012 at 1:41 pm
I enjoy what you guys are usually up too. This kind of clever work and exposure! Keep up the wonderful works guys I’ve included you guys to my personal blogroll.