[ZPT] Re: a little more on scripts execution

Arnar Lundesgaard arnar.lundesgaard@creuna.no
Mon, 5 Aug 2002 13:03:56 +0200


> Wait, wait.
> From what I can see you just redefined a variable and no matter
> what happened before "testScript" (".py" not allowed) always returns
> "Changed". In the end in a template you've got a variable that
renders to
> "Unchanged" string and a script which is going to return "Changed"
> regardless of any namespace issues.

Hmm, you are correct. It was not the best example. :-/

> In fact the question is: will the script see the "ns['text']"
construct
> or not ?

I have not been able to re-bind ns['text'] to another string or so,
but I
have not tried very hard so it may very well be possible. :-)

However, you can perform logic based on the value of ns['text'].

  if ns['text']=='Unchanged'
    return 'Warning'
  else:
    return 'All is well'



Another example/way around is to create a dictionary or list, and
manipulate that.

<html>
<head><title>Dictionary example</title></head>
<body>
  <span tal:define="text python:{'one':1,'two':2}">
    One: <span tal:replace="text/one" /><br>
    Two: <span tal:replace="text/two" /><br>
  <span tal:define="dummy here/testScript.py" /><br>
    One: <span tal:replace="text/one" /><br>
    Two: <span tal:replace="text/two" /><br>
  </span>
  <hr>
</body>
</html>

## Script (Python) "testScript.py"
##bind container=container
##bind context=context
##bind namespace=ns
##bind script=script
##bind subpath=traverse_subpath
##parameters=
##title=
##
text = ns['text']

if same_type(text, {}):
  text['one']='***'

return text


Which in ZOPE 2.5.1 gives me:
  One: 1
  Two: 2

  One: ***
  Two: 2




  -- Arnar Lundesgaard