mirror of
https://github.com/kennethreitz-archive/pystache.git
synced 2026-06-05 23:40:16 +00:00
Adding support for context nested on any flavoured object.
This commit is contained in:
@@ -58,10 +58,9 @@ class Template(object):
|
||||
|
||||
section, section_name, inner = match.group(0, 1, 2)
|
||||
section_name = section_name.strip()
|
||||
|
||||
it = self.view.get(section_name, None)
|
||||
replacer = ''
|
||||
|
||||
|
||||
# Callable
|
||||
if it and isinstance(it, collections.Callable):
|
||||
replacer = it(inner)
|
||||
@@ -73,6 +72,10 @@ class Template(object):
|
||||
elif it and hasattr(it, '__iter__'):
|
||||
if section[2] != '^':
|
||||
replacer = self._render_list(inner, it)
|
||||
# Other objects
|
||||
elif it and isinstance(it, object):
|
||||
if section[2] != '^':
|
||||
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
|
||||
|
||||
+11
-5
@@ -6,6 +6,9 @@ from examples.complex_view import ComplexView
|
||||
from examples.lambdas import Lambdas
|
||||
from examples.inverted import Inverted
|
||||
|
||||
class Thing(object):
|
||||
pass
|
||||
|
||||
class TestView(unittest.TestCase):
|
||||
def test_basic(self):
|
||||
view = Simple("Hi {{thing}}!", { 'thing': 'world' })
|
||||
@@ -86,11 +89,14 @@ class TestView(unittest.TestCase):
|
||||
view = Inverted()
|
||||
self.assertEquals(view.render(), """one, two, three, empty list""")
|
||||
|
||||
# def test_accessing_properties_on_parent_view(self):
|
||||
# view = Simple(context={'child':child})
|
||||
# view.template = '{{#child}}{{#t}}{{thing}}{{/t}}{{/child}}'
|
||||
#
|
||||
# self.assertEquals(view.render(), 'pizza1')
|
||||
def test_accessing_properties_on_parent_object_from_child_objects(self):
|
||||
parent = Thing()
|
||||
parent.this = 'derp'
|
||||
parent.children = [Thing()]
|
||||
view = Simple(context={'parent': parent})
|
||||
view.template = "{{#parent}}{{#children}}{{this}}{{/children}}{{/parent}}"
|
||||
|
||||
self.assertEquals(view.render(), 'derp')
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user