Use re.sub to replace template variables

This commit is contained in:
shortcutme 2019-08-23 03:39:50 +02:00
parent e16611f15a
commit 248fc5f015
No known key found for this signature in database
GPG key ID: 5B63BAE6CB9613AE

View file

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