diff options
author | Carl Whittaker <carl.whittaker@hogarthww.com> | 2011-01-11 17:14:56 +0000 |
---|---|---|
committer | Carl Whittaker <carl.whittaker@hogarthww.com> | 2011-01-11 17:14:56 +0000 |
commit | 01dd06b313bfae5a9fe6b2b1caf312c94c608f15 (patch) | |
tree | 9a51834b547591650aee18b0a28fea5579ad56f3 | |
parent | b7ab439a508cde02f1afd89da2f5835fe7684848 (diff) | |
download | pystache-01dd06b313bfae5a9fe6b2b1caf312c94c608f15.tar.gz |
Adding support for callables and Negated values.
-rw-r--r-- | examples/complex_view.py | 3 | ||||
-rw-r--r-- | pystache/template.py | 11 | ||||
-rw-r--r-- | tests/test_simple.py | 21 |
3 files changed, 32 insertions, 3 deletions
diff --git a/examples/complex_view.py b/examples/complex_view.py index f2106cb..ef45ff7 100644 --- a/examples/complex_view.py +++ b/examples/complex_view.py @@ -18,3 +18,6 @@ class ComplexView(pystache.View): def empty(self): return len(self.item()) == 0 + + def empty_list(self): + return []; diff --git a/pystache/template.py b/pystache/template.py index d4f9fd0..5faf7cd 100644 --- a/pystache/template.py +++ b/pystache/template.py @@ -61,13 +61,20 @@ class Template(object): it = view.get(section_name, None) replacer = '' + # Callable + if it and isinstance(it, collections.Callable): + replacer = it(inner) # Dictionary - if it and hasattr(it, 'keys') and hasattr(it, '__getitem__'): + elif it and hasattr(it, 'keys') and hasattr(it, '__getitem__'): if section[2] != '^': replacer = self._render_dictionary(inner, it) + # Lists elif it and hasattr(it, '__iter__'): if section[2] != '^': - replacer = self._render_list(inner, it) + replacer = self._render_list(inner, it) + # Falsey and Negated or Truthy and Not Negated + elif (not it and section[2] == '^') or (it and section[2] != '^'): + replacer = inner template = template.replace(section, replacer) diff --git a/tests/test_simple.py b/tests/test_simple.py index d60a0ed..edfa203 100644 --- a/tests/test_simple.py +++ b/tests/test_simple.py @@ -1,9 +1,28 @@ import unittest import pystache +from examples.nested_context import NestedContext +from examples.complex_view import ComplexView +from examples.lambdas import Lambdas class TestSimple(unittest.TestCase): def test_simple_render(self): tmpl = '{{derp}}' - self.assertEqual('herp', pystache.Template(tmpl, {'derp': 'herp'}).render())
\ No newline at end of file + self.assertEqual('herp', pystache.Template(tmpl, {'derp': 'herp'}).render()) + + def test_nested_context(self): + view = NestedContext() + self.assertEquals(pystache.Template('{{#foo}}{{thing1}} and {{thing2}} and {{outer_thing}}{{/foo}}{{^foo}}Not foo!{{/foo}}', view).render(), "one and foo and two") + + def test_looping_and_negation_context(self): + view = ComplexView() + self.assertEquals(pystache.Template('{{#item}}{{header}}: {{name}} {{/item}}{{^item}} Shouldnt see me{{/item}}', view).render(), "Colors: red Colors: green Colors: blue ") + + def test_empty_context(self): + view = ComplexView() + self.assertEquals(pystache.Template('{{#empty_list}}Shouldnt see me {{/empty_list}}{{^empty_list}}Should see me{{/empty_list}}', view).render(), "Should see me") + + def test_callables(self): + view = Lambdas() + self.assertEquals(pystache.Template('{{#replace_foo_with_bar}}foo != bar. oh, it does!{{/replace_foo_with_bar}}', view).render(), 'bar != bar. oh, it does!')
\ No newline at end of file |