Commit 85efd808 by Damien Lebrun Committed by Chris Wanstrath

View.template_path can hold a list of path

View.load_template start looking for the template
in first path of the list.
parent 83c0004e
...@@ -54,10 +54,24 @@ class View(object): ...@@ -54,10 +54,24 @@ class View(object):
if self.template: if self.template:
return self.template return self.template
if not self.template_file: if self.template_file:
return self._load_template()
name = self.get_template_name() + '.' + self.template_extension name = self.get_template_name() + '.' + self.template_extension
if isinstance(self.template_path, basestring):
self.template_file = os.path.join(self.template_path, name) self.template_file = os.path.join(self.template_path, name)
return self._load_template()
for path in self.template_path:
self.template_file = os.path.join(path, name)
if os.path.exists(self.template_file):
return self._load_template()
raise IOError('"%s" not found in "%s"' % (name, ':'.join(self.template_path),))
def _load_template(self):
f = open(self.template_file, 'r') f = open(self.template_file, 'r')
try: try:
template = f.read() template = f.read()
......
...@@ -17,6 +17,24 @@ class TestView(unittest.TestCase): ...@@ -17,6 +17,24 @@ class TestView(unittest.TestCase):
view = Simple(thing='world') view = Simple(thing='world')
self.assertEquals(view.render(), "Hi world!") self.assertEquals(view.render(), "Hi world!")
def test_template_load_from_multiple_path(self):
path = Simple.template_path
Simple.template_path = ('examples/nowhere','examples',)
try:
view = Simple(thing='world')
self.assertEquals(view.render(), "Hi world!")
finally:
Simple.template_path = path
def test_template_load_from_multiple_path_fail(self):
path = Simple.template_path
Simple.template_path = ('examples/nowhere',)
try:
view = Simple(thing='world')
self.assertRaises(IOError, view.render)
finally:
Simple.template_path = path
def test_basic_method_calls(self): def test_basic_method_calls(self):
view = Simple() view = Simple()
self.assertEquals(view.render(), "Hi pizza!") self.assertEquals(view.render(), "Hi pizza!")
......
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