I would like to use dtml-in to iterate through some results in a python script. I am not sure what format I need to put the results into in my python script to be able to reference them in the <dtml-in> tags. I am currently trying to return a dictionary but that does not seem to work. Here is how I have it setup. I have a DTML-method with a DTML-IN block that trys to printout the <dtml-var name> variable. <dtml-in currentItems> <dtml-var name> </dtml-in> The object currentItems is a python script. Here is a copy of it. I print out the return results and it goes back as a list with a dictionary per item. ## Script (Python) "current_items" ##bind container=container ##bind context=context ##bind namespace= ##bind script=script ##bind subpath=traverse_subpath ##parameters= ##title= ## # Get a list of the current items in the cart and return it back in a table format # Provide access to session data session=context.REQUEST.SESSION # Only get items if the cart has been established if session.has_key('cart'): cart=session.get('cart', {}) cart_items=[] # Lets loop through our cart and get each item for product_id, quantity in cart.items(): item=context.get_item(product_id=product_id).dictionaries() for record in item: record['quantity']=quantity #print record, "<br>" cart_items.append(record) print cart_items return printed #return cart_items else: # No Cart established, no need to return anything return Any help would be greatly appreciated. Thanks, -- Ben Bush Center 7, Systems Administrator 801-655-2640
On Thu, 2003-08-07 at 15:24, Ben Bush wrote:
I would like to use dtml-in to iterate through some results in a python script. I am not sure what format I need to put the results into in my python script to be able to reference them in the <dtml-in> tags. I am currently trying to return a dictionary but that does not seem to work.
You can't iterate over a dictionary... at least, not yet. :-) Instead, have the script return a *list* of items to iterate over. That should do the trick. HTH, Dylan
How do I address each item in the list? Can you give me an example if I were to return a list of integers and a dicitionary list. Here is what I would be interested in knowing -> Return [0,1,2,3] Can I get a way with returning dictionaries inside of a list? Return [{'field1':'value1'},{'field2':'value2'}] Now for the part that is killing me. How do I address the values in the <dtml-in script> tags. If I use <dtml-in zsql_method> it works just fine for me by allowing me to address each field by name (ie. <dtml-var description> if there was a description field in the database) How do I do that with the above examples. Thanks for your help. Dylan Reinhardt wrote:
On Thu, 2003-08-07 at 15:24, Ben Bush wrote:
I would like to use dtml-in to iterate through some results in a python script. I am not sure what format I need to put the results into in my python script to be able to reference them in the <dtml-in> tags. I am currently trying to return a dictionary but that does not seem to work.
You can't iterate over a dictionary... at least, not yet. :-)
Instead, have the script return a *list* of items to iterate over. That should do the trick.
HTH,
Dylan
On Thu, 2003-08-07 at 21:15, Ben Bush wrote:
How do I address each item in the list? Can you give me an example if I were to return a list of integers and a dicitionary list.
Sure. Search the online help for "in" for details. -------- <dtml-call "REQUEST.set('my_list', [{'foo': 1, 'bar':2}, {'foo': 3, 'bar':5}])"> <dtml-in my_list prefix=list> foo: <dtml-var "list_item['foo']"> bar: <dtml-var "list_item['bar']"> </dtml-in> <dtml-call "REQUEST.set('my_dict', {'foo': 7, 'bar':8})"> <dtml-in "my_dict.items()" prefix=dict> <dtml-var dict_key>: <dtml-var dict_item> </dtml-in> ------- Running the above should produce: foo: 1 bar: 2 foo: 3 bar: 5 foo: 7 bar: 8 HTH, Dylan
--On Donnerstag, 7. August 2003 16:24 Uhr -0600 Ben Bush <ben@center7.com> wrote:
I would like to use dtml-in to iterate through some results in a python script. I am not sure what format I need to put the results into in my python script to be able to reference them in the <dtml-in> tags. I am currently trying to return a dictionary but that does not seem to work.
You need to return a sequence (means a list or a tuple). If you return a dict, then you need to iterate over the keys() or values() or items() of the dict but *not* over the dict itself. CHeck the Python docs for details. -aj
participants (3)
-
Andreas Jung -
Ben Bush -
Dylan Reinhardt