Commit 89dd6059 by Chris Jerdonek

Removed the Template.template_path instance attribute.

Also simplified the Template.load_template() method.
parent 16aaa7f8
......@@ -10,15 +10,23 @@ import os
class Loader(object):
template_path = os.curdir # i.e. "."
def __init__(self, search_dirs=None, encoding=None, extension=None):
"""
Construct a template loader.
Arguments:
search_dirs: the directories in which to search for templates.
Defaults to the current working directory.
"""
if extension is None:
extension = 'mustache'
if search_dirs is None:
search_dirs = os.curdir # i.e. "."
if isinstance(search_dirs, basestring):
search_dirs = [search_dirs]
self.search_dirs = search_dirs
self.template_encoding = encoding
......@@ -31,24 +39,16 @@ class Loader(object):
Raises an IOError if the template cannot be found.
"""
template_dirs = self.search_dirs or self.template_path
search_dirs = self.search_dirs
file_name = template_name + '.' + self.template_extension
# Given a single directory, we'll load from it.
if isinstance(template_dirs, basestring):
file_path = os.path.join(template_dirs, file_name)
return self._load_template_file(file_path)
# Given a list of directories, we'll check each for our file.
for path in template_dirs:
file_path = os.path.join(path, file_name)
for dir_path in search_dirs:
file_path = os.path.join(dir_path, file_name)
if os.path.exists(file_path):
return self._load_template_file(file_path)
# TODO: we should probably raise an exception of our own type.
raise IOError('"%s" not found in "%s"' % (template_name, ':'.join(template_dirs),))
raise IOError('"%s" not found in "%s"' % (template_name, ':'.join(search_dirs),))
def _load_template_file(self, file_path):
"""
......
import os
import unittest
from pystache.loader import Loader
......@@ -11,7 +12,7 @@ class LoaderTestCase(unittest.TestCase):
"""
loader = Loader()
self.assertTrue(loader.search_dirs is None)
self.assertEquals(loader.search_dirs, [os.curdir])
self.assertTrue(loader.template_encoding is None)
self.assertEquals(loader.template_extension, 'mustache')
......
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