Permalink
Browse files

Fix template-searching logic, again.

  • Loading branch information...
cpressey committed Mar 8, 2013
1 parent f50f4f4 commit ecf1b1cc318b6c072d40dc4e56a77a22d391d609
Showing with 30 additions and 24 deletions.
  1. +30 −24 src/chrysoberyl/renderer.py
@@ -51,18 +51,17 @@ def get_template(self, key):
"""Helper method to retrieve the appropriate template for the given
key.
In each template directory, do the following until you find a template.
If the node is a type, look first for a template called type_[key].html,
in ALL the template directories. If found, load it as the template.
If not found, load type.html as the template.
If the node is a type, look first for a template called type_[key].html.
If none, just use type.html.
Look for a template named [key].html in ALL the template directories.
If found, use it as the template.
Otherwise, look for a template named [key].html.
If that's not found, look for a template called [type].html.
If that's not found, try the next template directory.
If all template directories are exhausted, fall back to base.html.
If no template yet, look for a template named [type].html in ALL the
template directories. If found, use it as the template.
If no template yet, use base.html as the template.
Note that this logic is not in the template loader, because it resolves
keys to filenames, and is not applicable during {% extends foo %}.
@@ -71,21 +70,28 @@ def get_template(self, key):
node = self.data[key]
# Mercurial can't handle filenames containing ':' on Windows, so:
key_filename = re.sub(':', '_', filekey(key))
for template_dir in self.template_dirs:
template_filename = None
if node['type'] == 'type':
if os.path.exists(os.path.join(template_dir, "type_" + key_filename)):
template_filename = "type_" + key_filename
else:
template_filename = 'type.html'
elif os.path.exists(os.path.join(template_dir, key_filename)):
template_filename = key_filename
def find_template(filename):
for template_dir in self.template_dirs:
full_filename = os.path.join(template_dir, filename)
if os.path.exists(full_filename):
return full_filename
return None
if node['type'] == 'type':
type_filename = 'type_' + key_filename
if find_template(type_filename):
return self.jinja2_env.get_template(type_filename)
else:
type_filename = filekey(node['type'])
if os.path.exists(os.path.join(template_dir, type_filename)):
template_filename = type_filename
if template_filename is not None:
return self.jinja2_env.get_template(template_filename)
return self.jinja2_env.get_template('type.html')
if find_template(key_filename):
return self.jinja2_env.get_template(key_filename)
type_filename = filekey(node['type'])
if find_template(type_filename):
return self.jinja2_env.get_template(type_filename)
return self.jinja2_env.get_template('base.html')
def render_node(self, key, node):

0 comments on commit ecf1b1c

Please sign in to comment.