Over a million developers have joined DZone.

Templating for Third Parties with Python’s string.Template

DZone's Guide to

Templating for Third Parties with Python’s string.Template

· Web Dev Zone ·
Free Resource

Building real-time chat? Enroll in a Free Course on Mobile Chat Development. 

I had a great conversation recently with Felix. At some point it was mentioned that you’d have to be a bit crazy (maybe good crazy) to decide to write your own templating system; there are already so many good options out there for almost every language. What was especially pertinent about the conversation, though, was that just yesterday the thought had crossed my mind that we might need to write our own simple templating system for Fiesta.

The problem

Our API tries to be as flexible as possible for developers. One aspect of that is allowing developers to use their own copy for some of the stock messages that Fiesta sends, like the “welcome” message that gets sent when a new member is added to a list. The problem is that some of those messages need to contain certain information that we want to be opaque to the API client (things like signed links, etc). The API client might get to decide where that information goes, but they don’t get to see the actual bits.

Sounds like a perfect job for a simple templating language: instead of sending us complete messages, the client sends Fiesta a template that gets filled in with the secret values. There doesn’t need to be any logic at all, just dead simple string substitution.

Our solution

My first thought was to just use Python’s replace method to replace predefined tokens with the right information. So we’d define tokens like {{ invite_url }}, and whenever those tokens appeared in the “template” we’d replace them with the proper value. This would be very simple, but could get a little messy as we add new tokens. Also, since it’s basically writing our own templating system, it’d only be a matter of time before we’d need to worry about handling escaping, invalid substitutions, etc.

I decided to use an existing templating system so we didn’t need to write anything new. We needed something that doesn’t allow logic in templates and is very simple to use (remember, our API users are the ones writing templates). I think the closest thing I found was Pystache, but even that seemed to have more functionality that we really needed (or wanted) to allow. That’s when I remembered that Python has basic string templates built-in. string.Template provides basic string substitution, with escaping. The default delimiter is $ (so tokens to get replaced look like $invite_url), but that is configurable as well. The best part is that it’s built-in, and no more difficult to use than the hacky replace call I contemplated above. Here’s the code that takes an incoming template from an API call and renders it with a generated “invite URL”:

template = string.Template(api_client_template)
text = template.safe_substitute(invite_url=invite_url)

And here’s what a client’s template might look like:

You've been invited to join a list! Click on the link below to accept the invitation:

I think string.Template is a great way to expose simple templates to third parties. Has anybody else dealt with a similar problem before? What did you come up with?


Source: http://blog.fiesta.cc/post/13502150064/using-pythons-string-template-to-provide-templating

Power realtime chat, IoT and messaging apps at scale. Pubsub realtime messaging, functions, chat, presence, push, notifications, blocks catalog and more.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}