Commit 3bcd6b63 by Paul Bonser Committed by Chris Wanstrath

add support for nested contexts as described in the "Non-False Values" section…

add support for nested contexts as described in the "Non-False Values" section of http://mustache.github.com/mustache.5.html
parent 7d7ddeb4
{{#foo}}{{thing1}} and {{thing2}}{{/foo}}{{^foo}}Not foo!{{/foo}}
\ No newline at end of file
import pystache
class NestedContext(pystache.View):
template_path = 'examples'
def foo(self):
return {'thing1': 'one', 'thing2': 'foo'}
...@@ -85,6 +85,9 @@ class Template(object): ...@@ -85,6 +85,9 @@ class Template(object):
elif it and not hasattr(it, '__iter__'): elif it and not hasattr(it, '__iter__'):
if section[2] != '^': if section[2] != '^':
replacer = inner replacer = inner
elif it and hasattr(it, 'keys') and hasattr(it, '__getitem__'):
if section[2] != '^':
replacer = self.render(inner, it)
elif it: elif it:
insides = [] insides = []
for item in it: for item in it:
......
...@@ -11,6 +11,7 @@ from examples.template_partial import TemplatePartial ...@@ -11,6 +11,7 @@ from examples.template_partial import TemplatePartial
from examples.delimiters import Delimiters from examples.delimiters import Delimiters
from examples.unicode_output import UnicodeOutput from examples.unicode_output import UnicodeOutput
from examples.unicode_input import UnicodeInput from examples.unicode_input import UnicodeInput
from examples.nested_context import NestedContext
class TestView(unittest.TestCase): class TestView(unittest.TestCase):
def test_comments(self): def test_comments(self):
...@@ -67,5 +68,8 @@ Again, Welcome! ...@@ -67,5 +68,8 @@ Again, Welcome!
* Then, surprisingly, it worked the third time. * Then, surprisingly, it worked the third time.
""") """)
def test_nested_context(self):
self.assertEquals(NestedContext().render(), "one and foo")
if __name__ == '__main__': if __name__ == '__main__':
unittest.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