Skip to content

Commit

Permalink
If and else blocks now cache variables.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jim Fulton committed Dec 31, 1997
1 parent 9f07cf6 commit 6ad3c19
Showing 1 changed file with 36 additions and 16 deletions.
52 changes: 36 additions & 16 deletions DT_If.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,14 @@
false text
<!--#/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.
'''

############################################################################
Expand Down Expand Up @@ -110,8 +117,8 @@
# (540) 371-6909
#
############################################################################
__rcs_id__='$Id: DT_If.py,v 1.5 1997/11/07 17:08:11 jim Exp $'
__version__='$Revision: 1.5 $'[11:-2]
__rcs_id__='$Id: DT_If.py,v 1.6 1997/12/31 20:32:11 jim Exp $'
__version__='$Revision: 1.6 $'[11:-2]

from DT_Util import *
import sys
Expand Down Expand Up @@ -149,18 +156,25 @@ def __init__(self, blocks):
self.sections.append((name, expr, section))

def render(self,md):
for name, expr, section in self.sections:
if expr is None:
try: v=md[name]
except KeyError, ev:
if ev is not name: raise KeyError, name, sys.exc_traceback
v=None
else:
v=expr.eval(md)

if v: return section(None,md)

if self.elses: return self.elses(None, md)
cache={}
md._push(cache)
try:
for name, expr, section in self.sections:
if expr is None:
try: v=md[name]
except KeyError, ev:
if ev is not name:
raise KeyError, name, sys.exc_traceback
v=None
else:
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 ''

Expand All @@ -183,14 +197,20 @@ def render(self,md):
except KeyError, ev:
if ev is not name: raise KeyError, name, sys.exc_traceback
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 ''

__call__=render

##########################################################################
#
# $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
# Changed so exception is raised if a sequence cannot be gotten during
# rendering.
Expand Down

0 comments on commit 6ad3c19

Please sign in to comment.