Hiding implementation details of the email templating system written in Python

I am writing an application in which one of the functions allows a user to write an email template using Markdown syntax.

Besides formatting, the user should be able to use placeholders for a pair of variables to be replaced at runtime.

The way it currently works is very simple: the templates have Pythonic% (var) placeholders and I replace them with a dictionary before applying the Markdown2 formatting.

It turns out that the end user of this system will be a tech-savvy user, and I would not want it to be clear that this is 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 reveal this to the user (I wish it was the same even if it was written in Java , Perl, Ruby, or whatever).

So, I would like to ask what you think would be the best way to expose placeholders to users:

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

  • What would be the best way to replace these placeholders?

I am using regex to modify - for example - $ {var} in% (var) s and then applying regular pattern substitution in Python, but I'm not sure what is the best approach.

If you go that way, it would be really nice if you could point me to what the draft of this regex is.

Thank!

Refresh . The user pointed out to use full blown templating systems, but I think it might not be worth it as I need to replace the replacement notes: I won't have loops or anything.

Final update . I decided not to use templating mechanisms at this time. I decided to use the simplified string.Template sequence (as pointed out in the hyperboreean comment ). The truth is, I don't like choosing a solution because it might be necessary in the future. I'll keep all of these suggestions up my sleeve, and if during the life of the app there is a clear need for one or more of the features they offer, I will revisit the idea. Right now, I really think it's overkill. Having full blown templates that the end usercan edit as he wants, at least from my point of view, more problems than good. Nevertheless, it is much more pleasant for me to realize the reasons why I did not go down this path, and not just not to study anything and not to choose.

Thanks a lot for all the input.

0


source to share


4 answers


Use a real template tool: mako or jinja . Don't fold yourself. Not worth it.



+6


source


You have a lightweight templating system ... I'm not sure if you can use some of the ones TurboGears provides (Kid or Genshi)



+2


source


I would recommend jinja2 .

It shouldn't create runtime performance issues as it compiles templates for python. This will provide much more flexibility. With regard to maintainability; it depends a lot on the coder, but in theory (and admittedly superficially) I don't understand why it's harder to maintain.

With a little extra work, you can give your tech-savvy user the ability to define the replacement style themselves. Or choose one of the defaults that you provide.

+1


source


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

This is slightly different from% formatting and may not be as instantly recognizable as Python:

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

      

0


source







All Articles