Edgewall Software

Ticket #584: two-template-error.py

File two-template-error.py, 1.2 KB (added by Jason R. Coombs <jaraco@…>, 10 years ago)

a more aggressive example with two templates

Line 
1import sys
2del sys.path[0]
3
4import os
5import genshi.template.loader
6import genshi.template.markup
7
8parent_src = """<doc
9        xmlns:py="http://genshi.edgewall.org/"
10        xmlns:xi="http://www.w3.org/2001/XInclude">
11        <py:for each="module in doc.get('children', [])">
12                <xi:include href="module.xml" />
13        </py:for>
14</doc>
15"""
16
17template_src = """<module
18        xmlns:py="http://genshi.edgewall.org/"
19        xmlns:xi="http://www.w3.org/2001/XInclude" py:strip="True">
20        <div py:content="module['name']" />
21        <py:for each="module in module.get('children', [])">
22                <!--! Here's where the recursion occurs -->
23                <xi:include href="module.xml" />
24        </py:for>
25</module>
26"""
27
28"""
29A module with a nested module
30"""
31module = dict(
32        type='module',
33        name='module 1',
34        children=[
35                dict(type='module', name='module 2'),
36        ],
37)
38doc = dict(
39        children=[module],
40)
41
42def test_file_loader():
43        try:
44                with open('module.xml', 'w') as template_file:
45                        template_file.write(template_src)
46                with open('doc.xml', 'w') as doc_file:
47                        doc_file.write(parent_src)
48                tmpl = genshi.template.loader.TemplateLoader(['.']).load('doc.xml')
49                print(tmpl.generate(doc=doc))
50        finally:
51                os.remove('module.xml')
52
53if __name__ == '__main__':
54        # infinite recursion with file_loader
55        test_file_loader()