Permalink
Browse files

adding support for implicit iterators

  • Loading branch information...
1 parent 442f84d commit feb339dae27ff0bc312f6029cc90acc7a1862b8b @mikeocool mikeocool committed Mar 8, 2011
Showing with 22 additions and 3 deletions.
  1. +6 −3 pystache/template.py
  2. +16 −0 tests/test_pystache.py
View
@@ -68,7 +68,7 @@ def compile_regexps(self):
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 @@ def render_tag(self, tag_name, context):
"""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 @@ def render_delimiter(self, tag_name=None, context=None):
"""Changes the Mustache delimiter."""
self.otag, self.ctag = tag_name.split(' ')
self.compile_regexps()
- return ''
+ return ''
View
@@ -78,6 +78,22 @@ def test_sections(self):
<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()

0 comments on commit feb339d

Please sign in to comment.