Commit 050ab67d by Chris Jerdonek

Completed issue #49: the Template class now manages the rendering context.

parent bcd4afe9
...@@ -9,6 +9,7 @@ import re ...@@ -9,6 +9,7 @@ import re
import cgi import cgi
import collections import collections
from .context import Context
from .loader import Loader from .loader import Loader
...@@ -63,24 +64,34 @@ class Template(object): ...@@ -63,24 +64,34 @@ class Template(object):
def __init__(self, template=None, context=None, **kwargs): def __init__(self, template=None, context=None, **kwargs):
""" """
The **kwargs arguments are only supported if the context is The context argument can be a dictionary, View, or Context instance.
a dictionary (i.e. not a View).
""" """
from .view import View from .view import View
self.template = template
if context is None: if context is None:
context = {} context = {}
if not isinstance(context, View): view = None
# Views do not support copy() or update().
if isinstance(context, View):
view = context
context = view.context.copy()
elif isinstance(context, Context):
context = context.copy() context = context.copy()
view = View(context=context, **kwargs)
else: else:
view = context # Otherwise, the context is a dictionary.
context = Context(context)
if kwargs:
context.push(kwargs)
if view is None:
view = View()
self.context = context
self.template = template
# The view attribute is used only for its load_template() method.
self.view = view self.view = view
self._compile_regexps() self._compile_regexps()
...@@ -113,7 +124,7 @@ class Template(object): ...@@ -113,7 +124,7 @@ class Template(object):
section, section_name, inner = match.group(0, 1, 2) section, section_name, inner = match.group(0, 1, 2)
section_name = section_name.strip() section_name = section_name.strip()
it = self.view.get(section_name, None) it = self.context.get(section_name, None)
replacer = '' replacer = ''
# Callable # Callable
...@@ -158,12 +169,13 @@ class Template(object): ...@@ -158,12 +169,13 @@ class Template(object):
return template return template
def _render_dictionary(self, template, context): def _render_dictionary(self, template, context):
self.view.context.push(context) self.context.push(context)
template = Template(template, self.view) template = Template(template, self.context)
template.view = self.view
out = template.render() out = template.render()
self.view.context.pop() self.context.pop()
return out return out
...@@ -176,7 +188,7 @@ class Template(object): ...@@ -176,7 +188,7 @@ class Template(object):
@modifiers.set(None) @modifiers.set(None)
def _render_tag(self, tag_name): def _render_tag(self, tag_name):
raw = self.view.get(tag_name, '') raw = self.context.get(tag_name, '')
# For methods with no return value # For methods with no return value
# #
...@@ -186,7 +198,7 @@ class Template(object): ...@@ -186,7 +198,7 @@ class Template(object):
# See issue #34: https://github.com/defunkt/pystache/issues/34 # See issue #34: https://github.com/defunkt/pystache/issues/34
if not raw and raw != 0: if not raw and raw != 0:
if tag_name == '.': if tag_name == '.':
raw = self.view.context.top() raw = self.context.top()
else: else:
return '' return ''
...@@ -199,7 +211,8 @@ class Template(object): ...@@ -199,7 +211,8 @@ class Template(object):
@modifiers.set('>') @modifiers.set('>')
def _render_partial(self, template_name): def _render_partial(self, template_name):
markup = self.view.load_template(template_name) markup = self.view.load_template(template_name)
template = Template(markup, self.view) template = Template(markup, self.context)
template.view = self.view
return template.render() return template.render()
@modifiers.set('=') @modifiers.set('=')
...@@ -220,7 +233,7 @@ class Template(object): ...@@ -220,7 +233,7 @@ class Template(object):
Render a tag without escaping it. Render a tag without escaping it.
""" """
return literal(self.view.get(tag_name, '')) return literal(self.context.get(tag_name, ''))
def render(self, encoding=None): def render(self, encoding=None):
""" """
......
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