Skip to content

Instantly share code, notes, and snippets.

@omaciel
Created July 30, 2014 17:45
Show Gist options
  • Save omaciel/635dd18c2bebde1c8104 to your computer and use it in GitHub Desktop.
Save omaciel/635dd18c2bebde1c8104 to your computer and use it in GitHub Desktop.
Python: Import on Demand
def render(engine_id, template, **kwargs):
engines = {
"jinja2": "jinja2",
"mako": "make.template",
}
try:
module = engines[engine_id]
engine = __import__(
module, globals(), locals(), ['Template'], -1)
except (KeyError, ImportError) as err:
print "Template engine not found!"
return ''
return engine.Template(template).render(**kwargs)
print render("jinja2", "Hi {{f}}", f="John")
print render("mako", "Hi ${f} ${l}", f="John", l="Doe")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment