[ZPT] TALES: how to access a list?

Raymond Penners raymond@duologix.nl
Thu, 04 Oct 2001 15:20:24 +0200


Hi,

I am definately in need of a Zope/ZPT guru here. I've spent all day 
trying, without any luck.

Question:
=========
Suppose I have a variable "mylist", which is, of course, of type list. 
How do I access elements of that list through tales?

"request/form/mylist/0" does not seem to work.

Background:
========
What I want to accomplish is the following: I have a form with a number 
of input fields, whose values are recorded as a list:

<li tal:repeat="num python:range(10)">
  <input type="text" name="mylist:list" value=""
    tal:attributes="value FIXME">
</li>

Here, FIXME is some TALES expression that inserts the value mylist[num]. 
If mylist[num] is not available (i.e. len(mylist)<=num), it should 
insert the empty string.

Approaches so far:
==================
1) use "python:here.REQUEST.form.get('myval')[num]" for FIXME.
This almost works, except that an exception occurs when num is larger 
than the length of myval.
Since the "python:" expression is a one-liner, I don't see how I can use 
a try/catch construction here.

2) I've tried stuff like:
- request/form/myval/0|nothing
- python:here.path('request/form/myval/%d' % num)

3) Another attempt:
python:test(here.path('request/form/myval') and 
len(here.path('request/form/myval'))>num, 
here.path('request/form/myval')[num], '')

Why this fails is beyond me. It seems that for "python:test(cond,a,b)" I 
can only use simple values for "a" and "b", and not a complete python 
expression like above. If I do so, I get all sorts of errors.

Help, anyone?

PS: It's a bit off-topic for this list, but if you also happen to know 
what the PythonScript equivalent for the following DTML is: "<dtml-var 
index_html>", please let me know. I've tried stuff like "return 
container.documument_src()", but failed... thanks.