[ZPT] Re: [Zope] ZPT: How do I see if something is in a sequence?

Evan Simpson evan@4-am.com
Wed, 15 Aug 2001 11:10:44 -0400


alan runyan wrote:
> <select name="moderator_ids:list" size="5" multiple>
>   <option tal:repeat="member container/portal_membership/listMembers"
>    tal:condition="python:'Moderator' in repeat.member.helplink_roles"
>    tal:content="python:member.getUserName()"> memberid
>   </option>
> </select>

Two things need changing here.  First, the condition must be on a tag 
inside of the tal:repeat's tag.  Remember, the order in which statements 
are evaluated is fixed, and is not affected by the order in which they 
appear on the tag.  Second, don't use 'repeat.member' to access element 
data. You only use 'repeat' to access things like the iteration number.

The business with the order of operations is painful, I'll admit.

So, like this:

<select name="moderator_ids:list" size="5" multiple>
   <div tal:repeat="member container/portal_membership/listMembers">
   <option tal:condition="python:'Moderator' in member.helplink_roles"
           tal:content="python:member.getUserName()">memberid
   </option>
   </div>
</select>

You should also be able to use:

tal:content="member/getUserName"

If you're using the most recent release of ZPT, you can pre-filter the 
list, like so:

<select name="moderator_ids:list" size="5" multiple
  tal:define="filter nocall:modules/ZTUtils/LazyFilter;
              members container/portal_membership/listMembers">
   <option tal:repeat="member python:filter(members, lambda 
x:'Moderator' in x.helplink_roles)"
           tal:content="member/getUserName">memberid
   </option>
</select>

All of this logic could be bundled up in a Script, too, like so:

## Script (Python) "getModerators"
moderators = []
for user in container.portal_membership.listMembers():
   if 'Moderator' in user.helplink_roles:
     moderators.append(user)
return moderators
# End Script

<select name="moderator_ids:list" size="5" multiple>
   <option tal:repeat="member container/getModerators"
           tal:content="member/getUserName">memberid
   </option>
</select>

Cheers,

Evan @ 4-am & Zope