Use re.sub to replace template variables

This commit is contained in:
shortcutme 2019-08-28 01:32:16 +02:00
parent 27a67d9753
commit a121c23973
No known key found for this signature in database
GPG Key ID: 5B63BAE6CB9613AE
1 changed files with 6 additions and 3 deletions

View File

@ -293,9 +293,12 @@ class UiRequest(object):
# Renders a template
def render(self, template_path, *args, **kwargs):
template = open(template_path).read()
for key, val in kwargs.items():
template = template.replace("{%s}" % key, "%s" % val)
return template.encode("utf8")
def renderReplacer(m):
return "%s" % kwargs.get(m.group(1), "")
template_rendered = re.sub("{(.*?)}", renderReplacer, template)
return template_rendered.encode("utf8")
# - Actions -