Commit b8ed0b67 by Chris Jerdonek

Tightened up the code in Template._render_tags().

* Used re.split() instead of re.search() to avoid string slicing.
* Used one "".join() instead of repeated string concatenation.
* Avoided duplicate logic on loop exit (i.e. output += template).
parent a301b62e
......@@ -169,21 +169,27 @@ class Template(object):
return template
def _render_tags(self, template):
output = ''
output = []
while True:
match = self.tag_re.search(template)
if match is None:
parts = self.tag_re.split(template, maxsplit=1)
output.append(parts[0])
if len(parts) < 2:
# Then there was no match.
break
tag, tag_type, tag_name = match.group(0, 1, 2)
start, tag_type, tag_name, template = parts
tag_name = tag_name.strip()
func = self.modifiers[tag_type]
replacement = func(self, tag_name)
output = output + template[0:match.start()] + replacement
template = template[match.end():]
tag_value = func(self, tag_name)
# Appending the tag value to the output prevents treating the
# value as a template string (bug: issue #44).
output.append(tag_value)
output = output + template
output = "".join(output)
return output
def _render_dictionary(self, template, context):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment