Skip to content

Commit

Permalink
View.template_path can hold a list of path
Browse files Browse the repository at this point in the history
View.load_template start looking for the template
in first path of the list.
  • Loading branch information
dinoboff authored and defunkt committed Mar 28, 2010
1 parent 83c0004 commit 85efd80
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
20 changes: 17 additions & 3 deletions pystache/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,25 @@ def __getitem__(self, attr):
def load_template(self):
if self.template:
return self.template

if not self.template_file:
name = self.get_template_name() + '.' + self.template_extension

if self.template_file:
return self._load_template()

name = self.get_template_name() + '.' + self.template_extension

if isinstance(self.template_path, basestring):
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')
try:
template = f.read()
Expand Down
18 changes: 18 additions & 0 deletions tests/test_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,24 @@ def test_kwargs(self):
def test_template_load(self):
view = Simple(thing='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):
view = Simple()
Expand Down

0 comments on commit 85efd80

Please sign in to comment.