summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Wanstrath <chris@ozmm.org>2009-10-29 01:14:30 -0700
committerChris Wanstrath <chris@ozmm.org>2009-10-29 01:14:30 -0700
commit68f7cca57911f2f0a219d5a478f53e3c7b29c2ef (patch)
tree78978977d3aeb0d038459774c367421545dd832b
parenta28dfaefcb3b64906b9bcfeb21dee67abd4dd108 (diff)
downloadpystache-68f7cca57911f2f0a219d5a478f53e3c7b29c2ef.tar.gz
sections work!
-rw-r--r--pystache/template.py15
-rw-r--r--tests/test_pystache.py12
2 files changed, 18 insertions, 9 deletions
diff --git a/pystache/template.py b/pystache/template.py
index 7214fb8..8d87cd9 100644
--- a/pystache/template.py
+++ b/pystache/template.py
@@ -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
diff --git a/tests/test_pystache.py b/tests/test_pystache.py
index dc7c023..9623190 100644
--- a/tests/test_pystache.py
+++ b/tests/test_pystache.py
@@ -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>
+""")