1
2
3
4
5
6
7
8
9
10
11
12
13 __doc__='''Conditional insertion
14
15 Conditional insertion is performed using 'if' and 'else'
16 commands.
17
18 To include text when an object is true using the EPFS
19 format, use::
20
21 %(if name)[
22 text
23 %(if name)]
24
25 To include text when an object is true using the HTML
26 format, use::
27
28 <!--#if name-->
29 text
30 <!--#/if name-->
31
32 where 'name' is the name bound to the object.
33
34 To include text when an object is false using the EPFS
35 format, use::
36
37 %(else name)[
38 text
39 %(else name)]
40
41 To include text when an object is false using the HTML
42 format, use::
43
44 <!--#else name-->
45 text
46 <!--#/else name-->
47
48 Finally to include text when an object is true and to
49 include different text when the object is false using the
50 EPFS format, use::
51
52 %(if name)[
53 true text
54 %(if name)]
55 %(else name)[
56 false text
57 %(else name)]
58
59 and to include text when an object is true and to
60 include different text when the object is false using the
61 HTML format, use::
62
63 <!--#if name-->
64 true text
65 <!--#else name-->
66 false text
67 <!--#/if name-->
68
69 Notes:
70
71 - if a variable is nor defined, it is considered to be false.
72
73 - A variable if only evaluated once in an 'if' tag. If the value
74 is used inside the tag, including in enclosed tags, the
75 variable is not reevaluated.
76
77 '''
78 __rcs_id__='$Id: DT_If.py 1069 2008-11-13 21:55:43Z stefan $'
79 __version__='$Revision: 1069 $'[11:-2]
80
81 from DT_Util import ParseError, parse_params, name_param, str
82
84 blockContinuations='else','elif'
85 name='if'
86 elses=None
87 expr=''
88
90
91 tname, args, section = blocks[0]
92 args=parse_params(args, name='', expr='')
93 name,expr=name_param(args,'if',1)
94 self.__name__= name
95 if expr is None: cond=name
96 else: cond=expr.eval
97 sections=[cond, section.blocks]
98
99 if blocks[-1][0]=='else':
100 tname, args, section = blocks[-1]
101 del blocks[-1]
102 args=parse_params(args, name='')
103 if args:
104 ename,expr=name_param(args,'else',1)
105 if ename != name:
106 raise ParseError, ('name in else does not match if', 'in')
107 elses=section.blocks
108 else: elses=None
109
110 for tname, args, section in blocks[1:]:
111 if tname=='else':
112 raise ParseError, (
113 'more than one else tag for a single if tag', 'in')
114 args=parse_params(args, name='', expr='')
115 name,expr=name_param(args,'elif',1)
116 if expr is None: cond=name
117 else: cond=expr.eval
118 sections.append(cond)
119 sections.append(section.blocks)
120
121 if elses is not None: sections.append(elses)
122
123 self.simple_form=tuple(sections)
124
136
140