[ZPT] Repeat over dict with options

David Pratt fairwinds at eastlink.ca
Wed Jun 2 09:59:02 EDT 2004


Hi Chis. Thank you for your reply.  This is the first time I am trying 
to use the predefined options variable and as you can guess it has been 
getting the best of me. I took your advice and am returning this from 
my Script python as opposed to RESPONSE.redirect with parameter passing.

> return context.test_results(results = {1:{'foo':'bar','foo2':'bar2'},
>                             2:{'foo':'barnone','foo2':'barnone2'}})

In my zpt, I have done this:

    <span tal:define="value options/results">
       <p>
          <span tal:content="value">value of results</span>
       </p>
     </span>

which  yields:

{1:{'foo':'bar','foo2':'bar2'}, 2:{'foo':'barnone','foo2':'barnone2'}

I was expecting to get each item of the dictionary returned on a 
separate line and then doing something to get the value based on the 
key like value.values()  and then another variable to get value of bar 
based on['foo']

My quest is how to get the values for foo and foo2 out of this so that 
the result is

       <p>
          bar
       </p>
       <p>
          bar2
       </p>

       <p>
          barnone
       </p>
       <p>
          barnone2
       </p>








On Wednesday, June 2, 2004, at 05:11 AM, Chris Withers wrote:

> David Pratt wrote:
>
>> from ZTUtils import make_query
>>     query = make_query(results = {'foo':'bar','foo2':'bar2'})
>>     return RESPONSE.redirect('test_results?%s' % query)
>
> that should really be:
>
> return RESPONSE.redirect('test_results?' + query)
>
>> This produces a url   http://blah.com/ 
>> test_results?results.foo:record=bar&results.foo2:record=bar2
>> If am not getting anything using a repeat with options/results.
>
> That's 'cos stuff in the REQUEST doesn't magically get put into 
> options. ZPT doesn't try and do any magic, which is a very good 
> thing...
>
> > How
>> should I construct the zpt to get the values of foo and foo2?
>> I have been trying iterations of something like this:
>>      <span tal:repeat="value  options/results">
>
> <span tal:repeat="value  request/form/results">
>
>> I noticed that the request object contains the dictionary I am passing
>
> No it doesn't, it contains a string, which happens to be the same as 
> the string representation of your dictionary.
>
>> Help much appreciated since I can't seem to get any further with this 
>>  in the meantime.
>
> I'd suggest changing your original python script to be:
>
> return context.test_results(results = {1:{'foo':'bar','foo2':'bar2'},
>                             2:{'foo':'barnone','foo2':'barnone2'}})
>
> ...now 'results' will be in options, as you originally expected, AND 
> it'll contain python types as you're expecting...
>
> cheers,
>
> Chris
>
> -- 
> Simplistix - Content Management, Zope & Python Consulting
>            - http://www.simplistix.co.uk
>




More information about the ZPT mailing list