Anonymous avatar Anonymous committed ea3d55e

Fix template-searching logic, again.

Comments (0)

Files changed (1)

src/chrysoberyl/renderer.py

         """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 %}.
         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):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.