Commit 499e3d2c by Chris Jerdonek

Merge branch 'issue_48' into development: closing issue #48

This also completes the milestone: "Template class should not depend on the
View class."
parents f54f0ea7 cbe677cc
......@@ -8,6 +8,9 @@ This module provides a Loader class.
import os
DEFAULT_EXTENSION = 'mustache'
class Loader(object):
def __init__(self, search_dirs=None, encoding=None, extension=None):
......@@ -21,7 +24,7 @@ class Loader(object):
"""
if extension is None:
extension = 'mustache'
extension = DEFAULT_EXTENSION
if search_dirs is None:
search_dirs = os.curdir # i.e. "."
......
......@@ -62,37 +62,38 @@ class Template(object):
modifiers = Modifiers()
def __init__(self, template=None, context=None, **kwargs):
def __init__(self, template=None, context=None, load_template=None, **kwargs):
"""
The context argument can be a dictionary, View, or Context instance.
Construct a Template instance.
"""
from .view import View
Arguments:
context: a dictionary, Context, or View instance.
load_template: the function for loading partials. The function should
accept a single template_name parameter and return a template as
a string. Defaults to the default Loader's load_template() method.
"""
if context is None:
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):
view = context
context = view.context.copy()
elif isinstance(context, Context):
if isinstance(context, Context):
context = context.copy()
else:
# Otherwise, the context is a dictionary.
context = Context(context)
if kwargs:
context.push(kwargs)
if view is None:
view = View()
self.context = context
self.load_template = load_template
self.template = template
# The view attribute is used only for its load_template() method.
self.view = view
self._compile_regexps()
......@@ -171,8 +172,7 @@ class Template(object):
def _render_dictionary(self, template, context):
self.context.push(context)
template = Template(template, self.context)
template.view = self.view
template = Template(template, self.context, self.load_template)
out = template.render()
self.context.pop()
......@@ -210,9 +210,8 @@ class Template(object):
@modifiers.set('>')
def _render_partial(self, template_name):
markup = self.view.load_template(template_name)
template = Template(markup, self.context)
template.view = self.view
markup = self.load_template(template_name)
template = Template(markup, self.context, self.load_template)
return template.render()
@modifiers.set('=')
......@@ -237,7 +236,7 @@ class Template(object):
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)
......
......@@ -19,7 +19,7 @@ class View(object):
template_path = None
template = None
template_encoding = None
template_extension = 'mustache'
template_extension = None
# A function that accepts a single template_name parameter.
_load_template = None
......@@ -93,7 +93,7 @@ class View(object):
Return the view rendered using the current context.
"""
template = Template(self.get_template(), self)
template = Template(self.get_template(), self.context, self.load_template)
return template.render(encoding=encoding)
def get(self, key, default=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