Commit caf67bd1 authored by Jim Fulton's avatar Jim Fulton

If and else blocks now cache variables.

parent 7299aa7c
...@@ -55,7 +55,14 @@ __doc__='''Conditional insertion ...@@ -55,7 +55,14 @@ __doc__='''Conditional insertion
false text false text
<!--#/if name--> <!--#/if name-->
Note that if a variable is nor defined, it is considered to be false. Notes:
- if a variable is nor defined, it is considered to be false.
- A variable if only evaluated once in an 'if' tag. If the value
is used inside the tag, including in enclosed tags, the
variable is not reevaluated.
''' '''
############################################################################ ############################################################################
...@@ -110,8 +117,8 @@ __doc__='''Conditional insertion ...@@ -110,8 +117,8 @@ __doc__='''Conditional insertion
# (540) 371-6909 # (540) 371-6909
# #
############################################################################ ############################################################################
__rcs_id__='$Id: DT_If.py,v 1.5 1997/11/07 17:08:11 jim Exp $' __rcs_id__='$Id: DT_If.py,v 1.6 1997/12/31 20:32:11 jim Exp $'
__version__='$Revision: 1.5 $'[11:-2] __version__='$Revision: 1.6 $'[11:-2]
from DT_Util import * from DT_Util import *
import sys import sys
...@@ -149,18 +156,25 @@ class If: ...@@ -149,18 +156,25 @@ class If:
self.sections.append((name, expr, section)) self.sections.append((name, expr, section))
def render(self,md): def render(self,md):
for name, expr, section in self.sections: cache={}
if expr is None: md._push(cache)
try: v=md[name] try:
except KeyError, ev: for name, expr, section in self.sections:
if ev is not name: raise KeyError, name, sys.exc_traceback if expr is None:
v=None try: v=md[name]
else: except KeyError, ev:
v=expr.eval(md) if ev is not name:
raise KeyError, name, sys.exc_traceback
if v: return section(None,md) v=None
else:
if self.elses: return self.elses(None, md) v=expr.eval(md)
cache[name]=v
if v: return section(None,md)
if self.elses: return self.elses(None, md)
finally: md._pop(1)
return '' return ''
...@@ -183,7 +197,10 @@ class Else: ...@@ -183,7 +197,10 @@ class Else:
except KeyError, ev: except KeyError, ev:
if ev is not name: raise KeyError, name, sys.exc_traceback if ev is not name: raise KeyError, name, sys.exc_traceback
v=None v=None
if not v: return self.section(None,md) if not v:
md._push({name:v})
try: return self.section(None,md)
finally: md._pop(1)
return '' return ''
__call__=render __call__=render
...@@ -191,6 +208,9 @@ class Else: ...@@ -191,6 +208,9 @@ class Else:
########################################################################## ##########################################################################
# #
# $Log: DT_If.py,v $ # $Log: DT_If.py,v $
# Revision 1.6 1997/12/31 20:32:11 jim
# If and else blocks now cache variables.
#
# Revision 1.5 1997/11/07 17:08:11 jim # Revision 1.5 1997/11/07 17:08:11 jim
# Changed so exception is raised if a sequence cannot be gotten during # Changed so exception is raised if a sequence cannot be gotten during
# rendering. # rendering.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment