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

Source Code for Module qm.external.DocumentTemplate.DT_With

 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  '''Nested namespace access 
14   
15     The 'with' tag is used to introduce nested namespaces. 
16   
17     The text enclosed in the with tag is rendered using information 
18     from the given variable or expression. 
19   
20     For example, if the variable 'person' is bound to an object that 
21     has attributes 'name' and 'age', then a 'with' tag like the 
22     following can be used to access these attributes:: 
23   
24       <!--#with person--> 
25         <!--#var name-->, 
26         <!--#var age--> 
27       <!--#/with--> 
28   
29     Eather a 'name' or an 'expr' attribute may be used to specify data. 
30     A 'mapping' attribute may be used to indicate that the given data 
31     should be treated as mapping object, rather than as an object with 
32     named attributes. 
33   
34  ''' 
35   
36  __rcs_id__='$Id: DT_With.py 1069 2008-11-13 21:55:43Z stefan $' 
37  __version__='$Revision: 1069 $'[11:-2] 
38   
39  from DT_Util import parse_params, name_param, InstanceDict, render_blocks, str 
40  from DT_Util import TemplateDict 
41 -class With:
42 blockContinuations=() 43 name='with' 44 mapping=None 45 only=0 46
47 - def __init__(self, blocks):
48 tname, args, section = blocks[0] 49 args=parse_params(args, name='', expr='', mapping=1, only=1) 50 name,expr=name_param(args,'with',1) 51 if expr is None: expr=name 52 else: expr=expr.eval 53 self.__name__, self.expr = name, expr 54 self.section=section.blocks 55 if args.has_key('mapping') and args['mapping']: self.mapping=1 56 if args.has_key('only') and args['only']: self.only=1
57
58 - def render(self, md):
59 expr=self.expr 60 if type(expr) is type(''): v=md[expr] 61 else: v=expr(md) 62 63 if not self.mapping: 64 if type(v) is type(()) and len(v)==1: v=v[0] 65 v=InstanceDict(v,md) 66 67 if self.only: 68 _md=md 69 md=TemplateDict() 70 if hasattr(_md, 'validate'): 71 md.validate=_md.validate 72 73 md._push(v) 74 try: return render_blocks(self.section, md) 75 finally: md._pop(1)
76 77 __call__=render
78