Commit 36e61711 by Carl Whittaker

Adding support for nested contexts within View.

parent 8954e01f
......@@ -8,3 +8,12 @@ class NestedContext(pystache.View):
def foo(self):
return {'thing1': 'one', 'thing2': 'foo'}
def derp(self):
return [{'inner': 'car'}]
def herp(self):
return [{'outer': 'car'}]
def nested_context_in_view(self):
return 'it works!' if self['outer'] == self['inner'] else ''
\ No newline at end of file
......@@ -2,6 +2,7 @@ import re
import cgi
import collections
import os
import copy
modifiers = {}
def modifier(symbol):
......@@ -96,15 +97,14 @@ class Template(object):
return template
def _render_dictionary(self, template, context):
from view import View
view = View(context=context)
view.template_path = self.view.template_path
view.template_encoding = self.view.template_encoding
view.parent = self.view
return Template(template, view).render()
original_context = copy.copy(self.view.context)
self.view.context.update(context)
out = Template(template, self.view).render()
self.view.context = original_context
return out
def _render_list(self, template, listing):
def _render_list(self, template, listing):
insides = []
for item in listing:
insides.append(self._render_dictionary(template, item))
......
......@@ -17,7 +17,7 @@ class View(object):
self.context.update(**kwargs)
def get(self, attr, default=None):
attr = self.context.get(attr, getattr(self, attr, self._get_from_parent(attr, default)))
attr = self.context.get(attr, getattr(self, attr, default))
if hasattr(attr, '__call__') and type(attr) is UnboundMethodType:
return attr()
if hasattr(attr, 'render'):
......@@ -25,12 +25,6 @@ class View(object):
else:
return attr
def _get_from_parent(self, attr, default=None):
if hasattr(self, 'parent'):
return self.parent.get(attr, default)
else:
return default
def get_template(self, template_name):
if not self.template:
from pystache import Loader
......@@ -55,4 +49,16 @@ class View(object):
return re.sub('[A-Z]', repl, template_name)[1:]
def render(self, encoding=None):
return Template(self.get_template(self.template_name), self).render(encoding=encoding)
\ No newline at end of file
return Template(self.get_template(self.template_name), self).render(encoding=encoding)
def __contains__(self, needle):
return needle in self.context or hasattr(self, needle)
def __getitem__(self, attr):
val = self.get(attr, None)
if not val:
raise KeyError("No such key.")
return val
def __str__(self):
return self.render()
\ No newline at end of file
......@@ -71,5 +71,10 @@ Again, Welcome!
def test_nested_context(self):
self.assertEquals(NestedContext().render(), "one and foo and two")
def test_nested_context_is_available_in_view(self):
view = NestedContext()
view.template = '{{#herp}}{{#derp}}{{nested_context_in_view}}{{/derp}}{{/herp}}'
self.assertEquals(view.render(), 'it works!')
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