From feb339dae27ff0bc312f6029cc90acc7a1862b8b Mon Sep 17 00:00:00 2001 From: Mike O'Toole Date: Tue, 8 Mar 2011 18:33:29 -0500 Subject: [PATCH 1/3] adding support for implicit iterators --- pystache/template.py | 9 ++++++--- tests/test_pystache.py | 16 ++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/pystache/template.py b/pystache/template.py index b2abdaa..0d0859f 100644 --- a/pystache/template.py +++ b/pystache/template.py @@ -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 diff --git a/tests/test_pystache.py b/tests/test_pystache.py index 3f21a45..0023f00 100644 --- a/tests/test_pystache.py +++ b/tests/test_pystache.py @@ -77,6 +77,22 @@ class TestPystache(unittest.TestCase): +""") + + def test_implicit_iterator(self): + template = """ + +""" + context = { 'users': [ 'Chris', 'Tom','PJ' ] } + ret = pystache.render(template, context) + self.assertEquals(ret, """ + """) if __name__ == '__main__': From de9f7d4110101b222a1f33d160874e36fae694c7 Mon Sep 17 00:00:00 2001 From: Yuest Wang Date: Wed, 20 Apr 2011 10:51:39 +0800 Subject: [PATCH 2/3] Inverted Sections For Empty List --- pystache/template.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pystache/template.py b/pystache/template.py index 0d0859f..96aaeca 100644 --- a/pystache/template.py +++ b/pystache/template.py @@ -89,7 +89,7 @@ class Template(object): elif it and hasattr(it, 'keys') and hasattr(it, '__getitem__'): if section[2] != '^': replacer = self.render(inner, it) - elif it: + elif it and section[2] != '^': insides = [] for item in it: insides.append(self.render(inner, item)) @@ -154,4 +154,4 @@ class Template(object): """Changes the Mustache delimiter.""" self.otag, self.ctag = tag_name.split(' ') self.compile_regexps() - return '' \ No newline at end of file + return '' From a78d3f65d6cf78a9c6efac0950d424eea642dfa4 Mon Sep 17 00:00:00 2001 From: Mark Paschal Date: Mon, 9 May 2011 18:22:36 -0700 Subject: [PATCH 3/3] Add test for inverted sections on list values --- examples/inverted.py | 9 +++++++++ tests/test_view.py | 6 +++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/examples/inverted.py b/examples/inverted.py index 1895050..660b932 100644 --- a/examples/inverted.py +++ b/examples/inverted.py @@ -11,3 +11,12 @@ class Inverted(pystache.View): def two(self): return 'two' + +class InvertedLists(Inverted): + template_name = 'inverted' + + def t(self): + return [0, 1, 2] + + def f(self): + return [] diff --git a/tests/test_view.py b/tests/test_view.py index 85ee8e4..814d2de 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 TestView(unittest.TestCase): def test_basic(self): @@ -90,6 +90,10 @@ class TestView(unittest.TestCase): view = Inverted() self.assertEquals(view.render(), """one, two, three""") + def test_inverted_lists(self): + view = InvertedLists() + self.assertEquals(view.render(), """one, two, three""") + if __name__ == '__main__': unittest.main()