Package qm :: Package external :: Package DocumentTemplate :: Module DT_If
[hide private]
[frames] | no frames]

Source Code for Module qm.external.DocumentTemplate.DT_If

  1  ############################################################################## 
  2  # 
  3  # Copyright (c) 2002 Zope Corporation and Contributors. All Rights Reserved. 
  4  # 
  5  # This software is subject to the provisions of the Zope Public License, 
  6  # Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution. 
  7  # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED 
  8  # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
  9  # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS 
 10  # FOR A PARTICULAR PURPOSE 
 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   
83 -class If:
84 blockContinuations='else','elif' 85 name='if' 86 elses=None 87 expr='' 88
89 - def __init__(self, blocks):
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
125 -class Unless:
126 name='unless' 127 blockContinuations=() 128
129 - def __init__(self, blocks):
130 tname, args, section = blocks[0] 131 args=parse_params(args, name='', expr='') 132 name,expr=name_param(args,'unless',1) 133 if expr is None: cond=name 134 else: cond=expr.eval 135 self.simple_form=(cond,None,section.blocks)
136
137 -class Else(Unless):
138 # The else tag is included for backward compatibility and is deprecated. 139 name='else'
140