Commit de8aed53 by Chris Jerdonek

Removed View from NestedContext example and added experimental Renderer.context property.

Also corrected some test variable names (expected -> actual). :)
parent 1996af1c
import pystache from pystache import TemplateSpec
class NestedContext(pystache.View): class NestedContext(TemplateSpec):
template_path = 'examples'
def __init__(self, renderer):
self.renderer = renderer
def _context_get(self, key):
return self.renderer.context.get(key)
def outer_thing(self): def outer_thing(self):
return "two" return "two"
...@@ -16,6 +21,6 @@ class NestedContext(pystache.View): ...@@ -16,6 +21,6 @@ class NestedContext(pystache.View):
return [{'outer': 'car'}] return [{'outer': 'car'}]
def nested_context_in_view(self): def nested_context_in_view(self):
if self.context.get('outer') == self.context.get('inner'): if self._context_get('outer') == self._context_get('inner'):
return 'it works!' return 'it works!'
return '' return ''
...@@ -111,6 +111,7 @@ class Renderer(object): ...@@ -111,6 +111,7 @@ class Renderer(object):
if isinstance(search_dirs, basestring): if isinstance(search_dirs, basestring):
search_dirs = [search_dirs] search_dirs = [search_dirs]
self._context = None
self.decode_errors = decode_errors self.decode_errors = decode_errors
self.escape = escape self.escape = escape
self.file_encoding = file_encoding self.file_encoding = file_encoding
...@@ -119,6 +120,19 @@ class Renderer(object): ...@@ -119,6 +120,19 @@ class Renderer(object):
self.search_dirs = search_dirs self.search_dirs = search_dirs
self.string_encoding = string_encoding self.string_encoding = string_encoding
# This is an experimental way of giving views access to the current context.
# TODO: consider another approach of not giving access via a property,
# but instead letting the caller pass the initial context to the
# main render() method by reference. This approach would probably
# be less likely to be misused.
@property
def context(self):
"""
Return the current rendering context [experimental].
"""
return self._context
def _to_unicode_soft(self, s): def _to_unicode_soft(self, s):
""" """
Convert a basestring to unicode, preserving any unicode subclass. Convert a basestring to unicode, preserving any unicode subclass.
...@@ -240,6 +254,7 @@ class Renderer(object): ...@@ -240,6 +254,7 @@ class Renderer(object):
template = self._to_unicode_hard(template) template = self._to_unicode_hard(template)
context = Context.create(*context, **kwargs) context = Context.create(*context, **kwargs)
self._context = context
engine = self._make_render_engine() engine = self._make_render_engine()
rendered = engine.render(template, context) rendered = engine.render(template, context)
......
...@@ -63,12 +63,18 @@ Again, Welcome!""") ...@@ -63,12 +63,18 @@ Again, Welcome!""")
""") """)
def test_nested_context(self): def test_nested_context(self):
self.assertEquals(NestedContext().render(), "one and foo and two") renderer = Renderer()
actual = renderer.render(NestedContext(renderer))
self.assertString(actual, u"one and foo and two")
def test_nested_context_is_available_in_view(self): def test_nested_context_is_available_in_view(self):
view = NestedContext() renderer = Renderer()
view = NestedContext(renderer)
view.template = '{{#herp}}{{#derp}}{{nested_context_in_view}}{{/derp}}{{/herp}}' view.template = '{{#herp}}{{#derp}}{{nested_context_in_view}}{{/derp}}{{/herp}}'
self.assertEquals(view.render(), 'it works!')
actual = renderer.render(view)
self.assertString(actual, u'it works!')
def test_partial_in_partial_has_access_to_grand_parent_context(self): def test_partial_in_partial_has_access_to_grand_parent_context(self):
view = TemplatePartial(context = {'prop': 'derp'}) view = TemplatePartial(context = {'prop': 'derp'})
......
...@@ -14,9 +14,12 @@ from tests.common import AssertStringMixin ...@@ -14,9 +14,12 @@ from tests.common import AssertStringMixin
class TestSimple(unittest.TestCase, AssertStringMixin): class TestSimple(unittest.TestCase, AssertStringMixin):
def test_nested_context(self): def test_nested_context(self):
view = NestedContext() renderer = Renderer()
view = NestedContext(renderer)
view.template = '{{#foo}}{{thing1}} and {{thing2}} and {{outer_thing}}{{/foo}}{{^foo}}Not foo!{{/foo}}' view.template = '{{#foo}}{{thing1}} and {{thing2}} and {{outer_thing}}{{/foo}}{{^foo}}Not foo!{{/foo}}'
self.assertEquals(view.render(), "one and foo and two")
actual = renderer.render(view)
self.assertString(actual, u"one and foo and two")
def test_looping_and_negation_context(self): def test_looping_and_negation_context(self):
template = '{{#item}}{{header}}: {{name}} {{/item}}{{^item}} Shouldnt see me{{/item}}' template = '{{#item}}{{header}}: {{name}} {{/item}}{{^item}} Shouldnt see me{{/item}}'
......
...@@ -88,8 +88,8 @@ class ViewTestCase(unittest.TestCase, AssertStringMixin): ...@@ -88,8 +88,8 @@ class ViewTestCase(unittest.TestCase, AssertStringMixin):
def test_complex(self): def test_complex(self):
renderer = Renderer() renderer = Renderer()
expected = renderer.render(Complex()) actual = renderer.render(Complex())
self.assertString(expected, u"""\ self.assertString(actual, u"""\
<h1>Colors</h1> <h1>Colors</h1>
<ul> <ul>
<li><strong>red</strong></li> <li><strong>red</strong></li>
...@@ -99,45 +99,45 @@ class ViewTestCase(unittest.TestCase, AssertStringMixin): ...@@ -99,45 +99,45 @@ class ViewTestCase(unittest.TestCase, AssertStringMixin):
def test_higher_order_replace(self): def test_higher_order_replace(self):
renderer = Renderer() renderer = Renderer()
expected = renderer.render(Lambdas()) actual = renderer.render(Lambdas())
self.assertEquals(expected, 'bar != bar. oh, it does!') self.assertEquals(actual, 'bar != bar. oh, it does!')
def test_higher_order_rot13(self): def test_higher_order_rot13(self):
view = Lambdas() view = Lambdas()
view.template = '{{#rot13}}abcdefghijklm{{/rot13}}' view.template = '{{#rot13}}abcdefghijklm{{/rot13}}'
renderer = Renderer() renderer = Renderer()
expected = renderer.render(view) actual = renderer.render(view)
self.assertString(expected, u'nopqrstuvwxyz') self.assertString(actual, u'nopqrstuvwxyz')
def test_higher_order_lambda(self): def test_higher_order_lambda(self):
view = Lambdas() view = Lambdas()
view.template = '{{#sort}}zyxwvutsrqponmlkjihgfedcba{{/sort}}' view.template = '{{#sort}}zyxwvutsrqponmlkjihgfedcba{{/sort}}'
renderer = Renderer() renderer = Renderer()
expected = renderer.render(view) actual = renderer.render(view)
self.assertString(expected, u'abcdefghijklmnopqrstuvwxyz') self.assertString(actual, u'abcdefghijklmnopqrstuvwxyz')
def test_partials_with_lambda(self): def test_partials_with_lambda(self):
view = Lambdas() view = Lambdas()
view.template = '{{>partial_with_lambda}}' view.template = '{{>partial_with_lambda}}'
renderer = Renderer(search_dirs=EXAMPLES_DIR) renderer = Renderer(search_dirs=EXAMPLES_DIR)
expected = renderer.render(view) actual = renderer.render(view)
self.assertEquals(expected, u'nopqrstuvwxyz') self.assertEquals(actual, u'nopqrstuvwxyz')
def test_hierarchical_partials_with_lambdas(self): def test_hierarchical_partials_with_lambdas(self):
view = Lambdas() view = Lambdas()
view.template = '{{>partial_with_partial_and_lambda}}' view.template = '{{>partial_with_partial_and_lambda}}'
renderer = Renderer(search_dirs=EXAMPLES_DIR) renderer = Renderer(search_dirs=EXAMPLES_DIR)
expected = renderer.render(view) actual = renderer.render(view)
self.assertString(expected, u'nopqrstuvwxyznopqrstuvwxyz') self.assertString(actual, u'nopqrstuvwxyznopqrstuvwxyz')
def test_inverted(self): def test_inverted(self):
renderer = Renderer() renderer = Renderer()
expected = renderer.render(Inverted()) actual = renderer.render(Inverted())
self.assertString(expected, u"""one, two, three, empty list""") self.assertString(actual, u"""one, two, three, empty list""")
def test_accessing_properties_on_parent_object_from_child_objects(self): def test_accessing_properties_on_parent_object_from_child_objects(self):
parent = Thing() parent = Thing()
...@@ -150,8 +150,8 @@ class ViewTestCase(unittest.TestCase, AssertStringMixin): ...@@ -150,8 +150,8 @@ class ViewTestCase(unittest.TestCase, AssertStringMixin):
def test_inverted_lists(self): def test_inverted_lists(self):
renderer = Renderer() renderer = Renderer()
expected = renderer.render(InvertedLists()) actual = renderer.render(InvertedLists())
self.assertString(expected, u"""one, two, three, empty list""") self.assertString(actual, u"""one, two, three, empty list""")
class SpecLoaderTests(unittest.TestCase, AssertIsMixin, AssertStringMixin): class SpecLoaderTests(unittest.TestCase, AssertIsMixin, AssertStringMixin):
......
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