Commit 8eac16d8 by Carl Whittaker

Adding support for context nested on any flavoured object.

parent c6f78bfd
......@@ -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
......
......@@ -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()
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