Commit ec120d99 by Chris Jerdonek

The View constructor now accepts a load_template.

Added a test case to load templates from a dictionary.
parent a4527728
...@@ -42,15 +42,17 @@ class View(object): ...@@ -42,15 +42,17 @@ class View(object):
template_encoding = None template_encoding = None
template_extension = 'mustache' template_extension = 'mustache'
template_loader = None # A function that accepts a single template_name parameter.
_load_template = None
def __init__(self, template=None, context=None, loader=None, **kwargs): def __init__(self, template=None, context=None, load_template=None, **kwargs):
""" """
Construct a View instance. Construct a View instance.
""" """
# TODO: add a unit test for passing a loader. if load_template is not None:
self.template_loader = loader self._load_template = load_template
self.template = template self.template = template
context = context or {} context = context or {}
...@@ -71,14 +73,16 @@ class View(object): ...@@ -71,14 +73,16 @@ class View(object):
return attr return attr
def load_template(self, template_name): def load_template(self, template_name):
if self.template_loader is None: if self._load_template is None:
# We delay setting the loader until now to allow users to set # We delay setting self._load_template until now (in the case
# the template_extension attribute, etc. after View.__init__() # that the user did not supply a load_template to the constructor)
# has already been called. # to let users set the template_extension attribute, etc. after
self.template_loader = Loader(search_dirs=self.template_path, encoding=self.template_encoding, # View.__init__() has already been called.
extension=self.template_extension) loader = Loader(search_dirs=self.template_path, encoding=self.template_encoding,
extension=self.template_extension)
return self.template_loader.load_template(template_name) self._load_template = loader.load_template
return self._load_template(template_name)
def get_template(self): def get_template(self):
""" """
......
...@@ -30,6 +30,19 @@ class TestView(unittest.TestCase): ...@@ -30,6 +30,19 @@ class TestView(unittest.TestCase):
template = Simple().load_template("escaped") template = Simple().load_template("escaped")
self.assertEquals(template, "<h1>{{title}}</h1>") self.assertEquals(template, "<h1>{{title}}</h1>")
def test_custom_load_template(self):
"""
Test passing a custom load_template to View.__init__().
"""
partials_dict = {"partial": "Loaded from dictionary"}
load_template = lambda template_name: partials_dict[template_name]
view = Simple(load_template=load_template)
actual = view.load_template("partial")
self.assertEquals(actual, "Loaded from dictionary")
def test_template_load_from_multiple_path(self): def test_template_load_from_multiple_path(self):
path = Simple.template_path path = Simple.template_path
Simple.template_path = ('examples/nowhere','examples',) Simple.template_path = ('examples/nowhere','examples',)
......
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