[ZPT] TALES: how to access a list?

Evan Simpson evan@zope.com
Thu, 04 Oct 2001 10:33:42 -0400


Raymond Penners wrote:

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


That's because "0" is treated as a string, not an integer.  There's no way 
to do this with pure paths, you need a Python expression.

> 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. 


test(), unfortunately, evaluates all of its arguments before choosing one, 
so your out-of-range index gets tried no matter what.  Python's 
short-circuiting booleans will do the job, tho:

tal:define="myval request/form/myval"
tal:attributes="value python:(num < len(myval) and myval[num]) or nothing"

> 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>"


Thanks to DTML's loose namespace rules, there isn't a guaranteed exact 
equivalent, but <tal:block replace="here/index_html" /> is probably what you 
want.


Cheers,

Evan @ Zope