Did you had a look at Jinja2 for templating?