Commit 77697374 by Rocky Duan

added support for applying lambda function after rendering

parent 599bb135
...@@ -41,7 +41,7 @@ def _compile_template_re(delimiters=None): ...@@ -41,7 +41,7 @@ def _compile_template_re(delimiters=None):
(?: (?:
(?P<change>=) \s* (?P<delims>.+?) \s* = | (?P<change>=) \s* (?P<delims>.+?) \s* = |
(?P<raw>{) \s* (?P<raw_name>.+?) \s* } | (?P<raw>{) \s* (?P<raw_name>.+?) \s* } |
(?P<tag>[%(tag_types)s]?) \s* (?P<tag_key>[\s\S]+?) (?P<tag>[%(tag_types)s]*) \s* (?P<tag_key>[\s\S]+?)
) )
\s* %(ctag)s \s* %(ctag)s
""" % {'tag_types': tag_types, 'otag': re.escape(delimiters[0]), 'ctag': re.escape(delimiters[1])} """ % {'tag_types': tag_types, 'otag': re.escape(delimiters[0]), 'ctag': re.escape(delimiters[1])}
...@@ -209,6 +209,11 @@ class Parser(object): ...@@ -209,6 +209,11 @@ class Parser(object):
parsed_section, section_contents, end_index = self._parse_section(template, end_index, tag_key) parsed_section, section_contents, end_index = self._parse_section(template, end_index, tag_key)
func = engine._make_get_section(tag_key, parsed_section, section_contents, self._delimiters) func = engine._make_get_section(tag_key, parsed_section, section_contents, self._delimiters)
elif tag_type == '##':
parsed_section, section_contents, end_index = self._parse_section(template, end_index, tag_key)
func = engine._make_rendered_get_section(tag_key, parsed_section, section_contents, self._delimiters)
elif tag_type == '^': elif tag_type == '^':
parsed_section, section_contents, end_index = self._parse_section(template, end_index, tag_key) parsed_section, section_contents, end_index = self._parse_section(template, end_index, tag_key)
......
...@@ -220,6 +220,38 @@ class RenderEngine(object): ...@@ -220,6 +220,38 @@ class RenderEngine(object):
return get_section return get_section
def _make_rendered_get_section(self, name, parsed_template_, template_, delims):
def get_section(context):
template = template_
parsed_template = parsed_template_
data = context.get(name)
if not data:
data = []
else:
try:
iter(data)
except TypeError:
data = [data]
else:
if isinstance(data, (basestring, dict)):
data = [data]
parts = []
for element in data:
if callable(element):
parts.append(element(parsed_template.render(context)))
continue
context.push(element)
parts.append(parsed_template.render(context))
context.pop()
return unicode(''.join(parts))
return get_section
def _parse(self, template, delimiters=None): def _parse(self, template, delimiters=None):
""" """
Parse the given template, and return a ParsedTemplate instance. Parse the given template, and return a ParsedTemplate instance.
......
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