I was using string.Template in WSGIKit recently, and I started hacking silly things
onto it with a dictionary that, for instance, turned d['str_v']
into repr(d['v']). But it got me to thinking about simple
templates and string substitution, and the next thing you know I
implemented something to do richer interpolation:
>>> name = 'Bob'
>>> e('Hi $name')
'Hi Bob'
>>> e('Hi ${name.lower()}')
'Hi bob'
>>> e('Your name is spelled: ${" ".join(list(name.upper()))}')
'Your name is spelled: B O B'
Maybe I should just use Cheetah and be
done with it, but it was interesting to experiment. The source to e is, I
think, relatively straight-forward. This has been done before in the
guise of PEP 215. If I
was to use this for real, I'd probably allow you to pass in a
namespace as well -- it still adds value over plain Template by
allowing expressions.