Commit feb339da by Mike O'Toole

adding support for implicit iterators

parent 442f84dd
......@@ -68,7 +68,7 @@ class Template(object):
tag = r"%(otag)s(#|=|&|!|>|\{)?(.+?)\1?%(ctag)s+"
self.tag_re = re.compile(tag % tags)
def render_sections(self, template, context):
"""Expands sections."""
while 1:
......@@ -121,7 +121,10 @@ class Template(object):
"""Given a tag name and context, finds, escapes, and renders the tag."""
raw = get_or_attr(context, tag_name, '')
if not raw and raw is not 0:
return ''
if tag_name == '.':
raw = context
else:
return ''
return cgi.escape(unicode(raw))
@modifier('!')
......@@ -151,4 +154,4 @@ class Template(object):
"""Changes the Mustache delimiter."""
self.otag, self.ctag = tag_name.split(' ')
self.compile_regexps()
return ''
return ''
\ No newline at end of file
......@@ -78,6 +78,22 @@ class TestPystache(unittest.TestCase):
<li>Chris</li><li>Tom</li><li>PJ</li>
</ul>
""")
def test_implicit_iterator(self):
template = """
<ul>
{{#users}}
<li>{{.}}</li>
{{/users}}
</ul>
"""
context = { 'users': [ 'Chris', 'Tom','PJ' ] }
ret = pystache.render(template, context)
self.assertEquals(ret, """
<ul>
<li>Chris</li><li>Tom</li><li>PJ</li>
</ul>
""")
if __name__ == '__main__':
unittest.main()
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