1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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()