batching/looping to find next item in python-scripts
I am trying to move all logic from DTML (not that I get it yet. :) I am now trying to generate the object that precedes the current object(zClass) in a folder. called by ...<a href"<dtml-var getPrevious>"> Previous </a> What am I doing wrong here?...I realize I am missing some fundamentals. # getPrevious Script (Python) # # determine the position of the current object y=0 for o in context.objectIds(): y=y+1 if sequence-item currentPosition=y ## loop to the previousItem and get ID for o in context.objectIds(): if sequence-item == currentPosition-1 previousObject=sequence-item.id return previousObject This is the DTML that doesnt quite work, that I am replacing. <dtml-in "objectIds()" start=qs size=1 orphan=0> <dtml-var sequence-item> <a href="&dtml-URL;?qs=<dtml-var "_.int(_['sequence-index'])+1">">Next</a> </dtml-in> ...likewise I will create a getNext. TIA for the lesson, -Trevor Toenjes
Trevor Toenjes writes:
What am I doing wrong here?...I realize I am missing some fundamentals.
# getPrevious Script (Python) # # determine the position of the current object y=0 for o in context.objectIds(): y=y+1 if sequence-item currentPosition=y
## loop to the previousItem and get ID for o in context.objectIds(): if sequence-item == currentPosition-1 previousObject=sequence-item.id
return previousObject Lots of things!
Basic Python syntax errors: all compound statement headerlines end in ':', not only "for" but also "if" and "else" "sequence-item" is "sequence" minus "item". Usually, you have the following correspondence (very weak!): DTML: <dtml-in sequence> ... <dtml-var sequence-item> ... </dtml-in> Python Script: for item in sequence: ... item ... But this makes no sense with your code. You "o" is an "id". It does not make sense to have "if o:" nor "if o == cuttentPosition-1". You must carefully think again.... Dieter
participants (2)
-
Dieter Maurer -
Trevor Toenjes