diff --git a/examples/inverted.py b/examples/inverted.py index daa6fca..e0f7aaf 100644 --- a/examples/inverted.py +++ b/examples/inverted.py @@ -16,4 +16,13 @@ class Inverted(pystache.View): return [] def populated_list(self): - return ['some_value'] \ No newline at end of file + return ['some_value'] + +class InvertedLists(Inverted): + template_name = 'inverted' + + def t(self): + return [0, 1, 2] + + def f(self): + return [] diff --git a/pystache/template.py b/pystache/template.py index 875f274..112219d 100644 --- a/pystache/template.py +++ b/pystache/template.py @@ -87,7 +87,7 @@ class Template(object): replacer = self._render_dictionary(inner, it) # Falsey and Negated or Truthy and Not Negated elif (not it and section[2] == '^') or (it and section[2] != '^'): - replacer = inner + replacer = self._render_dictionary(inner, it) template = template.replace(section, replacer) @@ -127,7 +127,10 @@ class Template(object): # For methods with no return value if not raw and raw is not 0: - return '' + if tag_name == '.': + raw = self.view.context_list[0] + else: + return '' return cgi.escape(unicode(raw)) diff --git a/tests/test_pystache.py b/tests/test_pystache.py index 6b82e3c..c04489b 100644 --- a/tests/test_pystache.py +++ b/tests/test_pystache.py @@ -68,6 +68,12 @@ class TestPystache(unittest.TestCase): context = { 'users': [ {'name': 'Chris'}, {'name': 'Tom'}, {'name': 'PJ'} ] } ret = pystache.render(template, context) self.assertEquals(ret, """""") + + def test_implicit_iterator(self): + template = """""" + context = { 'users': [ 'Chris', 'Tom','PJ' ] } + ret = pystache.render(template, context) + self.assertEquals(ret, """""") if __name__ == '__main__': unittest.main() diff --git a/tests/test_view.py b/tests/test_view.py index f714a19..a3464df 100644 --- a/tests/test_view.py +++ b/tests/test_view.py @@ -4,7 +4,7 @@ import pystache from examples.simple import Simple from examples.complex_view import ComplexView from examples.lambdas import Lambdas -from examples.inverted import Inverted +from examples.inverted import Inverted, InvertedLists class Thing(object): pass @@ -104,5 +104,9 @@ class TestView(unittest.TestCase): self.assertEqual(view.context, {'one': '1', 'two': '2'}) + def test_inverted_lists(self): + view = InvertedLists() + self.assertEquals(view.render(), """one, two, three, empty list""") + if __name__ == '__main__': unittest.main()