Commit bcd4afe9 by Chris Jerdonek

View class now uses new Context class.

parent c689ce93
......@@ -158,12 +158,12 @@ class Template(object):
return template
def _render_dictionary(self, template, context):
self.view.context_list.insert(0, context)
self.view.context.push(context)
template = Template(template, self.view)
out = template.render()
self.view.context_list.pop(0)
self.view.context.pop()
return out
......@@ -186,7 +186,7 @@ class Template(object):
# See issue #34: https://github.com/defunkt/pystache/issues/34
if not raw and raw != 0:
if tag_name == '.':
raw = self.view.context_list[0]
raw = self.view.context.top()
else:
return ''
......
......@@ -8,32 +8,11 @@ This module provides a View class.
import re
from types import UnboundMethodType
from .context import Context
from .loader import Loader
from .template import Template
def get_or_attr(context_list, name, default=None):
"""
Find and return an attribute from the given context.
"""
if not context_list:
return default
for obj in context_list:
try:
return obj[name]
except KeyError:
pass
except:
try:
return getattr(obj, name)
except AttributeError:
pass
return default
class View(object):
template_name = None
......@@ -50,31 +29,19 @@ class View(object):
Construct a View instance.
"""
if context is None:
context = {}
if load_template is not None:
self._load_template = load_template
if template is not None:
self.template = template
context = context or {}
context.update(**kwargs)
_context = Context(self)
if context:
_context.push(context)
if kwargs:
_context.push(kwargs)
self.context_list = [context]
def get(self, attr, default=None):
"""
Return the value for the given attribute.
"""
attr = get_or_attr(self.context_list, attr, getattr(self, attr, default))
if hasattr(attr, '__call__') and type(attr) is UnboundMethodType:
return attr()
else:
return attr
self.context = _context
def load_template(self, template_name):
if self._load_template is None:
......@@ -121,13 +88,6 @@ class View(object):
return re.sub('[A-Z]', repl, template_name)[1:]
def _get_context(self):
context = {}
for item in self.context_list:
if hasattr(item, 'keys') and hasattr(item, '__getitem__'):
context.update(item)
return context
def render(self, encoding=None):
"""
Return the view rendered using the current context.
......@@ -136,14 +96,8 @@ class View(object):
template = Template(self.get_template(), self)
return template.render(encoding=encoding)
def __contains__(self, needle):
return needle in self.context or hasattr(self, needle)
def __getattr__(self, attr):
if attr == 'context':
return self._get_context()
raise AttributeError("Attribute '%s' does not exist in View" % attr)
def get(self, key, default=None):
return self.context.get(key, default)
def __str__(self):
return self.render()
......@@ -173,12 +173,6 @@ class ViewTestCase(unittest.TestCase):
self.assertEquals(view.render(), 'derp')
def test_context_returns_a_flattened_dict(self):
view = Simple()
view.context_list = [{'one':'1'}, {'two':'2'}, object()]
self.assertEqual(view.context, {'one': '1', 'two': '2'})
def test_inverted_lists(self):
view = InvertedLists()
self.assertEquals(view.render(), """one, two, three, empty list""")
......
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