views:

166

answers:

4

I am writing an application where one of the features is to allow the user to write an email template using Markdown syntax.

Besides formatting, the user must be able to use placeholders for a couple of variables that would get replaced at runtime.

The way this is currently working is very simple: the templates have the Pythonic %(var)s placeholders and I replace those with a dictionary before applying Markdown2 formatting.

Turns out that the end user of this system will be a tech-savvy user and I wouldn't like to make it obvious to everyone that it's written in Python.

It's not that I don't like Python... I actually think Python is the perfect tool for the job, I just don't want to expose that to the user (would like the same even if it were written in Java, Perl, Ruby or anything else).

So I'd like to ask for insights on what would be, in your opinion, the best way to expose placeholders for the users:

  1. What do you think is the best placeholder format (thinks like ${var}, $(var) or #{var})?

  2. What would be the best way to replace those placeholders?

I though of using a Regular Expression to change - for instance - ${var} into %(var)s and then applying the regular Python templating substitution, but I am not sure that's the best approach.

If you go that way, it would be very nice if you could indicate me what is a draft of that regular expression as well.

Thanks!

Update: An user pointed out using full-blown templating systems, but I think that may not be worth it, since all I need is placeholders substitution: I won't have loops or anything like that.

Final Update: I have chosen not to use any template engines at this time. I chose to go with the simpler string.Template approach (as pointed out on a comment by hyperboreean). Truth is that I don't like to pick a solution because sometime in the future there may be a need. I will keep all those suggestions on my sleeve, and if on the lifespan of the application there is a clear need for one or more features offered by them, I'll revisit the idea. Right now, I really think it's an overkill. Having full blown templates that the end user can edit as he wants is, at least on my point of view, more trouble than benefit. Nevertheless, it feels much nicer being aware of the reasons I did not went down that path, than just not researching anything and choosing it.

Thanks a lot for all the input.

+6  A: 

Use a real template tool: mako or jinja. Don't roll your own. Not worth it.

S.Lott
Isn't that overkill for just placeholders?
kolrie
Nope. These things always expand. Write as little unique code as possible; use as much of someone else's code as you can.
S.Lott
ps: Jinja2 has pretty much replaced Jinja
Ali A
I ended up using jinja2, which is awesome.
kolrie
+2  A: 

Have a light templating system ... I am not sure if you can use some of the ones TurboGears provides (Kid or Genshi)

hyperboreean
Genshi looks promising! I will leave this open for more insight, but once I am done investigating I will probably accept your answer. Thanks!
kolrie
+1  A: 

I would recommend jinja2.

It shouldn't create runtime performance issue since it compiles templates to python. It would offer much greater flexibility. As for maintainability; it depends much on the coder, but theoretically (and admittedly superficially) I can't see why it should be harder to maintain.

With a little more work you can give your tech-savvy user the ability to define the substitution style herself. Or choose one from defaults you supply.

muhuk
A: 

You could try Python 2.6/3.0's new str.format() method: http://docs.python.org/library/string.html#formatstrings

This looks a bit different to %-formatting and might not be as instantly recognisable as Python:

'fish{chips}'.format(chips= 'x')
bobince