Commit cbe677cc by Chris Jerdonek

Addressed issue #48: "Template class should not depend on the View class"

The template module no longer imports from the view module.
parent ccfa6075
...@@ -64,40 +64,33 @@ class Template(object): ...@@ -64,40 +64,33 @@ class Template(object):
def __init__(self, template=None, context=None, load_template=None, **kwargs): def __init__(self, template=None, context=None, load_template=None, **kwargs):
""" """
Construct a Template instance.
Arguments: Arguments:
context: a dictionary, View, or Context instance. context: a dictionary, Context, or View instance.
load_template: a function that accepts a single template_name load_template: the function for loading partials. The function should
parameter and returns a template as a string. Defaults to the accept a single template_name parameter and return a template as
default Loader's load_template() method. a string. Defaults to the default Loader's load_template() method.
""" """
from .view import View
if context is None: if context is None:
context = {} context = {}
view = None if load_template is None:
loader = Loader()
load_template = loader.load_template
load_template = getattr(context, 'load_template', load_template)
if isinstance(context, View): if isinstance(context, Context):
view = context
context = view.context.copy()
load_template = view.load_template
elif isinstance(context, Context):
context = context.copy() context = context.copy()
else: else:
# Otherwise, the context is a dictionary.
context = Context(context) context = Context(context)
if kwargs: if kwargs:
context.push(kwargs) context.push(kwargs)
if load_template is None:
loader = Loader()
load_template = loader.load_template
self.context = context self.context = context
self.load_template = load_template self.load_template = load_template
self.template = template self.template = template
...@@ -243,7 +236,7 @@ class Template(object): ...@@ -243,7 +236,7 @@ class Template(object):
def render(self, encoding=None): def render(self, encoding=None):
""" """
Return the template rendered using the current view context. Return the template rendered using the current context.
""" """
template = self._render_sections(self.template) template = self._render_sections(self.template)
......
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