Commit 050ab67d by Chris Jerdonek

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

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