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. ...@@ -8,6 +8,9 @@ This module provides a Loader class.
import os import os
DEFAULT_EXTENSION = 'mustache'
class Loader(object): class Loader(object):
def __init__(self, search_dirs=None, encoding=None, extension=None): def __init__(self, search_dirs=None, encoding=None, extension=None):
...@@ -21,7 +24,7 @@ class Loader(object): ...@@ -21,7 +24,7 @@ class Loader(object):
""" """
if extension is None: if extension is None:
extension = 'mustache' extension = DEFAULT_EXTENSION
if search_dirs is None: if search_dirs is None:
search_dirs = os.curdir # i.e. "." search_dirs = os.curdir # i.e. "."
......
...@@ -62,37 +62,38 @@ class Template(object): ...@@ -62,37 +62,38 @@ class Template(object):
modifiers = Modifiers() 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.
""" Arguments:
from .view import View
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: 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()
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 view is None:
view = View()
self.context = context self.context = context
self.load_template = load_template
self.template = template self.template = template
# The view attribute is used only for its load_template() method.
self.view = view
self._compile_regexps() self._compile_regexps()
...@@ -171,8 +172,7 @@ class Template(object): ...@@ -171,8 +172,7 @@ class Template(object):
def _render_dictionary(self, template, context): def _render_dictionary(self, template, context):
self.context.push(context) self.context.push(context)
template = Template(template, self.context) template = Template(template, self.context, self.load_template)
template.view = self.view
out = template.render() out = template.render()
self.context.pop() self.context.pop()
...@@ -210,9 +210,8 @@ class Template(object): ...@@ -210,9 +210,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.load_template(template_name)
template = Template(markup, self.context) template = Template(markup, self.context, self.load_template)
template.view = self.view
return template.render() return template.render()
@modifiers.set('=') @modifiers.set('=')
...@@ -237,7 +236,7 @@ class Template(object): ...@@ -237,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)
......
...@@ -19,7 +19,7 @@ class View(object): ...@@ -19,7 +19,7 @@ class View(object):
template_path = None template_path = None
template = None template = None
template_encoding = None template_encoding = None
template_extension = 'mustache' template_extension = None
# A function that accepts a single template_name parameter. # A function that accepts a single template_name parameter.
_load_template = None _load_template = None
...@@ -93,7 +93,7 @@ class View(object): ...@@ -93,7 +93,7 @@ class View(object):
Return the view rendered using the current context. 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) return template.render(encoding=encoding)
def get(self, key, default=None): 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