Last active
December 18, 2015 09:38
-
-
Save ivan-krukov/5762279 to your computer and use it in GitHub Desktop.
Template string wrapper. uses 'inspect' to get the caller namespace dict
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
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