quick search:

Pass multiple values from python script to DTML

Submitted by: ksmithcw
Last Edited: 2001-08-09

Category: Python(Script)

Average rating is: 0.0 out of 5 (0 ratings)

Pass multiple values from python script to DTML. Taken from a mailing list post by Duncan Booth

If you want to return a single value, just return it and use dtml-var to
insert it into your document at one location, or dtml-let to save the
value for use in several places.
<dtml-var myscript>
PythonScript myscript:
return "return value"

If you want to return multiple values, then return a namespace
object and use dtml-with to make the values available for use. The
example below shows the technique I prefer, which is to make all
python scripts return a namespace.

Source (Text):
For example, the following code is taken from a simple shopping
basket system. basketSummary displays a one line summary of
the basket contents:
   This method returns an HTML segment that displays a summary
   of the contents of the current shopping basket.
   <dtml-with "logic.basketContents()">
   <p class="small">Shopping Basket: <dtml-var numlines> lines,
   <dtml-var numitems> items, Total Value: <dtml-var totalvalue

The python script basketContents, kept in the logic folder, retrieves
the current contents of the basket and looks like this:

if not context.REQUEST.has_key('SESSION'):

basket = context.REQUEST.SESSION.get('basket', None) or []
customer = container.customerData()

#  Return the basket contents.
contents = []
totalvalue, items = 0.0, 0
for c, d, u, q, p in basket:
    contents.append(namespace(code=c, description=d, unitprice=u,
        quantity=q, price=p)[0])
    totalvalue, items = totalvalue + p, items + q
shipping = 4.95
total = totalvalue + shipping

vars = namespace(contents=contents, totalvalue=totalvalue,
    shipping=shipping, total=total, numlines=len(contents), numitems=items,
return vars

Notice that for the summary display we are just interested in the
totals. The code that does the full display can get at the individual
lines using dtml-in:
   <dtml-with "logic.basketContents()">
       <dtml-in contents>
            <dtml-var description> <dtml-var unitprice> etc.
      </dtml-in contents>

Note that the namespace function actually returns a tuple
containing the namespace as the first (only) element. This is fine
when returning the final result, but for building a list of namespaces
you have to remove the extra tuple wrapping, hence the [0] in the
append call (and also the customer[0] since customer was the
result of another call to namespace).

Duncan Booth                                             duncan@rcp.co.uk


No Comments