Created
July 30, 2014 17:45
-
-
Save omaciel/635dd18c2bebde1c8104 to your computer and use it in GitHub Desktop.
Python: Import on Demand
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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