Commit cc18da30 by Chris Jerdonek

Fixed issue #40.

parent 9b087b28
No file extension: {{foo}}
\ No newline at end of file
......@@ -22,6 +22,9 @@ class Loader(object):
search_dirs: the directories in which to search for templates.
Defaults to the current working directory.
extension: the template file extension. Defaults to "mustache".
Pass False for no extension.
"""
if extension is None:
extension = DEFAULT_EXTENSION
......@@ -35,6 +38,13 @@ class Loader(object):
self.template_encoding = encoding
self.template_extension = extension
def make_file_name(self, template_name):
file_name = template_name
if self.template_extension is not False:
file_name += os.path.extsep + self.template_extension
return file_name
def load_template(self, template_name):
"""
Find and load the given template, and return it as a string.
......@@ -43,7 +53,8 @@ class Loader(object):
"""
search_dirs = self.search_dirs
file_name = template_name + '.' + self.template_extension
file_name = self.make_file_name(template_name)
for dir_path in search_dirs:
file_path = os.path.join(dir_path, file_name)
......
......@@ -6,6 +6,8 @@ from pystache.loader import Loader
class LoaderTestCase(unittest.TestCase):
search_dirs = 'examples'
def test_init(self):
"""
Test the __init__() constructor.
......@@ -14,13 +16,34 @@ class LoaderTestCase(unittest.TestCase):
loader = Loader()
self.assertEquals(loader.search_dirs, [os.curdir])
self.assertTrue(loader.template_encoding is None)
self.assertEquals(loader.template_extension, 'mustache')
loader = Loader(search_dirs=['foo'], encoding='utf-8', extension='txt')
loader = Loader(search_dirs=['foo'], encoding='utf-8')
self.assertEquals(loader.search_dirs, ['foo'])
self.assertEquals(loader.template_encoding, 'utf-8')
def test_init__extension(self):
# Test the default value.
loader = Loader()
self.assertEquals(loader.template_extension, 'mustache')
loader = Loader(extension='txt')
self.assertEquals(loader.template_extension, 'txt')
loader = Loader(extension=False)
self.assertTrue(loader.template_extension is False)
def test_make_file_name(self):
loader = Loader()
loader.template_extension = 'bar'
self.assertEquals(loader.make_file_name('foo'), 'foo.bar')
loader.template_extension = False
self.assertEquals(loader.make_file_name('foo'), 'foo')
loader.template_extension = ''
self.assertEquals(loader.make_file_name('foo'), 'foo.')
def test_template_is_loaded(self):
loader = Loader(search_dirs='examples')
template = loader.load_template('simple')
......@@ -37,3 +60,10 @@ class LoaderTestCase(unittest.TestCase):
loader = Loader()
self.assertRaises(IOError, loader.load_template, 'doesnt_exist')
def test_load_template__extensionless_file(self):
loader = Loader(search_dirs=self.search_dirs)
self.assertRaises(IOError, loader.load_template, 'extensionless')
loader.template_extension = False
self.assertEquals(loader.load_template('extensionless'), "No file extension: {{foo}}")
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