Skip to content

Instantly share code, notes, and snippets.

@ivan-krukov
Last active December 18, 2015 09:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivan-krukov/5762279 to your computer and use it in GitHub Desktop.
Save ivan-krukov/5762279 to your computer and use it in GitHub Desktop.
Template string wrapper. uses 'inspect' to get the caller namespace dict
import inspect
def render(string,variables=None):
"""Wrapper method for String Template:
name="Jeff"
render("Hello, $name")
>>Hello, Jeff
"""
t = Template(string)
if not variables:
frame = inspect.currentframe()
try:
variables = frame.f_back.f_locals
finally:
del frame
return t.substitute(variables)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment