« Contingent DIV blocks in ZPT - a design flaw. | Main | XML declaration and CSS support in IE »

April 21, 2005

Testing for the presence of objects in Zope

Here's a ZPT way to check for the presence of a particular folder in Zope:

<tal:block tal:define="
  folders python:context.objectValues('Folder');
" tal:repeat="folder python:folders">
<tal:block tal:define="
  this_id python:folder.getId();
  folder_name python:record['foaf_nick_name'];
" tal:condition="python:this_id == folder_name">
<tal:block tal:condition="python: root.new_site.contact.is_rstx_here(folder=folder,test_id='role_stx') == 1
">
<h3>Role and responsibilities</h3>
<tal:block tal:define="
  rstx python:modules['Products'].PythonScripts.standard.restructured_text;
  content_text python: folder.role_stx;
" tal:replace="structure python:rstx(content_text)"/>
</tal:block>
</tal:block>
</tal:block>

The following Python does the same but checks for rstx docs or DTML methods and is called as is_rstx_here() above:

for item in folder.objectValues('ReStructuredText Document'):
  this_id = item.getId()
  if this_id == str(test_id):
    return 1
for item in folder.objectValues('DTML Method'):
  this_id = item.getId()
  if this_id == str(test_id):
    return 1
return 0

Tags: ZPT

Posted by pj at April 21, 2005 04:31 PM