Question

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.

Was it helpful?

Solution

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

OTHER TIPS

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

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.

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')
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top