Getting counts from a ZCatalog - it must be easier than my feeble attempt!
Hi all, I need to find out how many results would be returned from a search, without necessarily getting the results set itself. Currently the only way I can do this is to iterate through a zcatalog using <dtml-in> and add up the number of items. <dtml-var> does not support functions such as len(), but I'm convinced I'm missing something, and there is a considerably more elegant solution to this one... Here's my code: 1: <P><b>Subjects</b><br> 2: <dtml-in expr="('SubjectContext'),('AudienceContext')"> 3: <dtml-let sIndex=sequence-key> 4: <dtml-in expr="articleIndex.uniqueValuesFor(sIndex)" > 5: <dtml-call "REQUEST.set('nItemCount',0)"> 6: <dtml-in expr="articleIndex({sIndex:_.getitem('sequence-item')})"> 7: <dtml-call "REQUEST.set('nItemCount',nItemCount+1)"> 8: </dtml-in> 9: <a href="/index_html?<dtml-var sIndex>=<dtml-var sequence-item>"> 10: <dtml-var sequence-item capitalize> 11: </a> 12: (<dtml-var nItemCount>)<br> 13: </dtml-in> 14: </dtml-let> 15: </dtml-in> 16: </P> which outputs a side menu bar looking something along these lines: Subjects: Assessment (2) Content (3) Metadata (5) For Developers (5) For Educators (3) For Members (2) What I'm looking for is something like: <dtml-var expr="len(articleIndex({'SubjectContext':_.getitem('sequence-item')})[0])"> to replace lines 5,6,7. Unfortunately this gives me an error. Any ideas? Thanks, Scott Zope Newbie
[S. Wilson] | <dtml-var | expr="len(articleIndex({'SubjectContext':_.getitem('sequence-item')})[0])"> Isn't that because you have that [0] added to the back of that sequence?
The sticking point is that "len" is not a recognised namespace item when called from dtml-var. The [0] actually refers to the first MyBrain instance in the LazyMap that this method returns when called from within a Python script. As an aside, if you just want to get the id of the first item in a results set in a python script you can use mycatalog({myquery})[0][0] - occasionally handy even if it does look like a hack. For example in my custom 404 page: # # see if the last element of the url after the / is the name of a Group # in the Group catalog. # if so, redirect the user to the homepage for that Group # sPath = request.URLPATH0 sPath = sPath[-(len(sPath)-1):] if (context.groupCatalog.searchResults({'Name':sPath}) ): groupid = context.groupCatalog({'Name':sPath})[0][0] sUrl='/templates/group?groupid=' + groupid response.redirect(sUrl,lock=1) Erik Enge wrote:
| <dtml-var | expr="len(articleIndex({'SubjectContext':_.getitem('sequence-item')})[0])">
Isn't that because you have that [0] added to the back of that sequence?
participants (2)
-
Erik Enge -
S.Wilson