re: Length of results set
Hi again, Maybe I'd better re-phrase my question. I think it will be easy for many of you, maybe even me.. I had too much coffee last night. How do you get the length of results for a <dtml-if> inside a <dtml-in> ? It can't really have anything to do with <sequence-length>, and I've tried a few ideas using "._len(...)" but to no avail. In Beno's now famous example, how to report the number of objects where "folder==lang" ?
<dtml-let lang="REQUEST.get('HTTP_ACCEPT_LANGUAGE','en-us')"> <dtml-in objectValues> <dtml-let folder=id> <dtml-let langShort=lang[2:]> <dtml-var langShort> <dtml-if expr="folder==lang"> <dtml-var lang> </dtml-if> </dtml-let> </dtml-let> </dtml-in> </dtml-let>
Like Beno, I'll write a Python script next time, I promise... Thanks! Ken
At 08:39 AM 1/16/2003, Ken wrote:
Hi again,
Maybe I'd better re-phrase my question. I think it will be easy for many of you, maybe even me.. I had too much coffee last night.
How do you get the length of results for a <dtml-if> inside a <dtml-in> ?
It can't really have anything to do with <sequence-length>, and I've tried a few ideas using "._len(...)" but to no avail.
It's _.len() not ._len() The underscore is a namespace representing the current context. You need to call len() as a method of that namespace. You will also need to use the prefix attribute of <dtml-in> since you can't use "sequence-item" in a Python expression. Do something like this: <dtml-in some_sequence prefix="my_seq"> <dtml-if "_.len(my_seq_item) > 5"> It's bigger than five. <dtml-else> It's less than or equal to five </dtml-if> </dtml-in> HTH, Dylan
Ken wrote at 2003-1-16 17:39 +0100:
Maybe I'd better re-phrase my question. I think it will be easy for many of you, maybe even me.. I had too much coffee last night.
How do you get the length of results for a <dtml-if> inside a <dtml-in> ? You do *NOT* do this in DTML!
In Python, you simple count all positive hits in a loop: i= 0 for o in context.objectValues(): if ...: i+= 1 return i or you use "list comprehension" (see Python documentation) and "len" return len([o for o in context.objectValues() if ...]) You see, how easy it is in Python? Dieter
participants (3)
-
Dieter Maurer -
Dylan Reinhardt -
Ken