comparison genshi/template/tests/loader.py @ 696:66eead58c120 trunk

More flexible template loader allowing for loading from package data and dispatching to different template directories based on path prefix. Can be easily extended for using custom template loading. Closes #182.
author cmlenz
date Wed, 26 Mar 2008 22:49:23 +0000
parents 9f11c745fac9
children 408ab81767c7
comparison
equal deleted inserted replaced
695:ed5044d318ed 696:66eead58c120
102 tmpl = loader.load('sub/tmpl2.html') 102 tmpl = loader.load('sub/tmpl2.html')
103 self.assertEqual("""<html> 103 self.assertEqual("""<html>
104 <div>Included</div> 104 <div>Included</div>
105 </html>""", tmpl.generate().render()) 105 </html>""", tmpl.generate().render())
106 106
107 def test_relative_include_samesubdir(self):
108 file1 = open(os.path.join(self.dirname, 'tmpl1.html'), 'w')
109 try:
110 file1.write("""<div>Included tmpl1.html</div>""")
111 finally:
112 file1.close()
113
114 os.mkdir(os.path.join(self.dirname, 'sub'))
115 file2 = open(os.path.join(self.dirname, 'sub', 'tmpl1.html'), 'w')
116 try:
117 file2.write("""<div>Included sub/tmpl1.html</div>""")
118 finally:
119 file2.close()
120
121 file3 = open(os.path.join(self.dirname, 'sub', 'tmpl2.html'), 'w')
122 try:
123 file3.write("""<html xmlns:xi="http://www.w3.org/2001/XInclude">
124 <xi:include href="tmpl1.html" />
125 </html>""")
126 finally:
127 file3.close()
128
129 loader = TemplateLoader([self.dirname])
130 tmpl = loader.load('sub/tmpl2.html')
131 self.assertEqual("""<html>
132 <div>Included sub/tmpl1.html</div>
133 </html>""", tmpl.generate().render())
134
107 def test_relative_include_without_search_path(self): 135 def test_relative_include_without_search_path(self):
108 file1 = open(os.path.join(self.dirname, 'tmpl1.html'), 'w') 136 file1 = open(os.path.join(self.dirname, 'tmpl1.html'), 'w')
109 try: 137 try:
110 file1.write("""<div>Included</div>""") 138 file1.write("""<div>Included</div>""")
111 finally: 139 finally:
169 </html>""", filename='subdir/tmpl2.html', loader=loader) 197 </html>""", filename='subdir/tmpl2.html', loader=loader)
170 198
171 self.assertEqual("""<html> 199 self.assertEqual("""<html>
172 <div>Included</div> 200 <div>Included</div>
173 </html>""", tmpl2.generate().render()) 201 </html>""", tmpl2.generate().render())
202
203 def test_relative_absolute_template_preferred(self):
204 file1 = open(os.path.join(self.dirname, 'tmpl1.html'), 'w')
205 try:
206 file1.write("""<div>Included</div>""")
207 finally:
208 file1.close()
209
210 os.mkdir(os.path.join(self.dirname, 'sub'))
211 file2 = open(os.path.join(self.dirname, 'sub', 'tmpl1.html'), 'w')
212 try:
213 file2.write("""<div>Included from sub</div>""")
214 finally:
215 file2.close()
216
217 file3 = open(os.path.join(self.dirname, 'sub', 'tmpl2.html'), 'w')
218 try:
219 file3.write("""<html xmlns:xi="http://www.w3.org/2001/XInclude">
220 <xi:include href="tmpl1.html" />
221 </html>""")
222 finally:
223 file3.close()
224
225 loader = TemplateLoader()
226 tmpl = loader.load(os.path.abspath(os.path.join(self.dirname, 'sub',
227 'tmpl2.html')))
228 self.assertEqual("""<html>
229 <div>Included from sub</div>
230 </html>""", tmpl.generate().render())
174 231
175 def test_load_with_default_encoding(self): 232 def test_load_with_default_encoding(self):
176 f = open(os.path.join(self.dirname, 'tmpl.html'), 'w') 233 f = open(os.path.join(self.dirname, 'tmpl.html'), 'w')
177 try: 234 try:
178 f.write(u'<div>\xf6</div>'.encode('iso-8859-1')) 235 f.write(u'<div>\xf6</div>'.encode('iso-8859-1'))
217 tmpl = loader.load('tmpl.html') 274 tmpl = loader.load('tmpl.html')
218 self.assertEqual("""<html> 275 self.assertEqual("""<html>
219 <p>Hello, hello</p> 276 <p>Hello, hello</p>
220 </html>""", tmpl.generate().render()) 277 </html>""", tmpl.generate().render())
221 278
279 def test_prefix_delegation_to_directories(self):
280 dir1 = os.path.join(self.dirname, 'templates')
281 os.mkdir(dir1)
282 file1 = open(os.path.join(dir1, 'foo.html'), 'w')
283 try:
284 file1.write("""<div>Included foo</div>""")
285 finally:
286 file1.close()
287
288 dir2 = os.path.join(self.dirname, 'sub1', 'templates')
289 os.makedirs(dir2)
290 file2 = open(os.path.join(dir2, 'tmpl1.html'), 'w')
291 try:
292 file2.write("""<html xmlns:xi="http://www.w3.org/2001/XInclude">
293 <xi:include href="foo.html" /> from sub1
294 </html>""")
295 finally:
296 file2.close()
297
298 dir3 = os.path.join(self.dirname, 'sub2', 'templates')
299 os.makedirs(dir3)
300 file3 = open(os.path.join(dir3, 'tmpl2.html'), 'w')
301 try:
302 file3.write("""<html xmlns:xi="http://www.w3.org/2001/XInclude">
303 <xi:include href="foo.html" /> from sub2
304 </html>""")
305 finally:
306 file3.close()
307
308 loader = TemplateLoader([dir1, TemplateLoader.prefixed(
309 sub1 = os.path.join(dir2),
310 sub2 = os.path.join(dir3)
311 )])
312 tmpl = loader.load('sub1/tmpl1.html')
313 self.assertEqual("""<html>
314 <div>Included foo</div> from sub1
315 </html>""", tmpl.generate().render())
316
222 317
223 def suite(): 318 def suite():
224 suite = unittest.TestSuite() 319 suite = unittest.TestSuite()
225 suite.addTest(doctest.DocTestSuite(TemplateLoader.__module__)) 320 suite.addTest(doctest.DocTestSuite(TemplateLoader.__module__))
226 suite.addTest(unittest.makeSuite(TemplateLoaderTestCase, 'test')) 321 suite.addTest(unittest.makeSuite(TemplateLoaderTestCase, 'test'))
Copyright (C) 2012-2017 Edgewall Software