Pastie now auto-senses if line-wrap is a bad or good idea. Feedback?
## mark a section (Learn more)
This paste will be private.
Index: genshi/tests/template.py =================================================================== --- genshi/tests/template.py (revision 346) +++ genshi/tests/template.py (working copy) @@ -1164,7 +1164,23 @@ <div>Included</div> </html>""", tmpl.generate().render()) + def test_relative_include_from_inmemory_template(self): + file1 = open(os.path.join(self.dirname, 'tmpl1.html'), 'w') + try: + file1.write("""<div>Included</div>""") + finally: + file1.close() + loader = TemplateLoader([self.dirname]) + tmpl2 = MarkupTemplate("""<html xmlns:xi="http://www.w3.org/2001/XInclude"> + <xi:include href="../tmpl1.html" /> + </html>""", filename='subdir/tmpl2.html', loader=loader) + + self.assertEqual("""<html> + <div>Included</div> + </html>""", tmpl2.generate().render()) + + def suite(): suite = unittest.TestSuite() suite.addTest(doctest.DocTestSuite(template)) Index: genshi/template.py =================================================================== --- genshi/template.py (revision 346) +++ genshi/template.py (working copy) @@ -1314,7 +1314,7 @@ directly @param cls: the class of the template object to instantiate """ - if relative_to: + if relative_to and not os.path.isabs(relative_to): filename = os.path.join(os.path.dirname(relative_to), filename) filename = os.path.normpath(filename) @@ -1329,12 +1329,21 @@ except KeyError: pass - # Bypass the search path if the filename is absolute search_path = self.search_path + if os.path.isabs(filename): + # Bypass the search path if the requested filename is absolute search_path = [os.path.dirname(filename)] - if not search_path: + elif relative_to and os.path.isabs(relative_to): + # Make sure that the directory containing the including + # template is on the search path + dirname = os.path.dirname(relative_to) + if dirname not in search_path: + search_path = search_path[:] + [dirname] + + elif not search_path: + # Uh oh, don't know where to look for the template raise TemplateError('Search path for templates not configured') for dirname in search_path: @@ -1352,7 +1361,7 @@ except IOError: continue - raise TemplateNotFound(filename, self.search_path) + raise TemplateNotFound(filename, search_path) finally: self._lock.release()
From the Design Piracy series on my blog: