Commit 68f7cca5 by Chris Wanstrath

sections work!

parent a28dfaef
......@@ -19,15 +19,24 @@ class Template(object):
return self.render_tags(template, context)
def render_sections(self, template, context):
regexp = re.compile(r"{{\#([^\}]*)}}\s*(.+?){{/\1}}")
"""Expands sections."""
regexp = re.compile(r"{{\#([^\}]*)}}\s*(.+?)\s*{{/\1}}", re.M | re.S)
match = re.search(regexp, template)
while match:
section, section_name, inner = match.group(0, 1, 2)
if section_name in context and context[section_name]:
return template.replace(section, inner)
if hasattr(context[section_name], '__iter__'):
insides = ''
for item in context[section_name]:
ctx = context.copy()
ctx.update(item)
insides += self.render(inner, ctx)
template = template.replace(section, insides)
else:
template = template.replace(section, inner)
else:
return template.replace(section, '')
template = template.replace(section, '')
match = re.search(regexp, template)
return template
......
......@@ -31,7 +31,7 @@ class TestPystache(unittest.TestCase):
ret = Pystache.render(template, { 'set': True })
self.assertEquals(ret, "Ready set go!")
def aaxtest_sections(self):
def test_sections(self):
template = """
<ul>
{{#users}}
......@@ -42,8 +42,8 @@ class TestPystache(unittest.TestCase):
context = { 'users': [ {'name': 'Chris'}, {'name': 'Tom'}, {'name': 'PJ'} ] }
ret = Pystache.render(template, context)
self.assertEquals(ret, """<ul>
<li>Chris</li>
<li>Tom</li>
<li>PJ</li>
</ul>""")
self.assertEquals(ret, """
<ul>
<li>Chris</li><li>Tom</li><li>PJ</li>
</ul>
""")
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