changeset 301:e3ee88d5113f stable-0.3.x 0.3.3

Ported [370] to 0.3.x branch.
author cmlenz
date Mon, 16 Oct 2006 08:11:47 +0000
parents 3df37819534b
children f1569069b6e8
files genshi/template.py genshi/tests/template.py
diffstat 2 files changed, 43 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/genshi/template.py
+++ b/genshi/template.py
@@ -1340,17 +1340,20 @@
             pass
 
         search_path = self.search_path
+        isabs = False
 
         if os.path.isabs(filename):
             # Bypass the search path if the requested filename is absolute
             search_path = [os.path.dirname(filename)]
+            isabs = True
 
         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]
+                search_path = search_path + [dirname]
+            isabs = True
 
         elif not search_path:
             # Uh oh, don't know where to look for the template
@@ -1361,6 +1364,14 @@
             try:
                 fileobj = open(filepath, 'U')
                 try:
+                    if isabs:
+                        # If the filename of either the included or the 
+                        # including template is absolute, make sure the
+                        # included template gets an absolute path, too,
+                        # so that nested include work properly without a
+                        # search path
+                        filename = os.path.join(dirname, filename)
+                        dirname = ''
                     tmpl = cls(fileobj, basedir=dirname, filename=filename,
                                loader=self)
                 finally:
--- a/genshi/tests/template.py
+++ b/genshi/tests/template.py
@@ -1174,6 +1174,37 @@
               <div>Included</div>
             </html>""", tmpl.generate().render())
 
+    def test_relative_include_without_search_path_nested(self):
+        file1 = open(os.path.join(self.dirname, 'tmpl1.html'), 'w')
+        try:
+            file1.write("""<div>Included</div>""")
+        finally:
+            file1.close()
+
+        file2 = open(os.path.join(self.dirname, 'tmpl2.html'), 'w')
+        try:
+            file2.write("""<div xmlns:xi="http://www.w3.org/2001/XInclude">
+              <xi:include href="tmpl1.html" />
+            </div>""")
+        finally:
+            file2.close()
+
+        file3 = open(os.path.join(self.dirname, 'tmpl3.html'), 'w')
+        try:
+            file3.write("""<html xmlns:xi="http://www.w3.org/2001/XInclude">
+              <xi:include href="tmpl2.html" />
+            </html>""")
+        finally:
+            file3.close()
+
+        loader = TemplateLoader()
+        tmpl = loader.load(os.path.join(self.dirname, 'tmpl3.html'))
+        self.assertEqual("""<html>
+              <div>
+              <div>Included</div>
+            </div>
+            </html>""", tmpl.generate().render())
+
     def test_relative_include_from_inmemory_template(self):
         file1 = open(os.path.join(self.dirname, 'tmpl1.html'), 'w')
         try:
Copyright (C) 2012-2017 Edgewall Software