I use custom widgets as "portlets" on every site I have developed with TurboGears. I use the following base class:

class Portlet(Widget):
   title = None
   visible = True
   args = None
   params = ['title', 'visible', 'args']
   params_doc = {
      'title': 'The title of the portlet.',
'visible': 'Only display the widget if this value is true. Always pass a callable.'
   }

   def display(self, value=None, **params):
      if not self.visible: return None
      return super(Portlet, self).display(value=value, **params)

An example of a static portlet (from GothCandy.com, Japanese removed):

class ProfilePortlet(Portlet):
   title = "Profile"
   template = """
      <div xmlns:py="http://purl.org/kid/ns#"; class="portlet tags">
         <h2>profile</h2>
         <p class="profile"><strong>Name:</strong>Alice McGregor</p>
<p class="profile"><strong>Location:</strong>British Columbia, CA</p> <p class="profile quote">Growing up in a small town is tough when you're this strange.</p>
         <!-- ... -->
      </div>
   """

An example of a slightly less static portlet, for the blog portion of GothCandy.com:

class TagPortlet(Portlet):
   title = "Tags"
   template = """
      <div xmlns:py="http://purl.org/kid/ns#"; class="portlet tags">
         <h2>Tags</h2>
         <p>This entry has been tagged with the following keywords:</p>
<ul class="tags" py:if="not tg.config('identity.on', False) or tg.identity.anonymous">
            <li py:for="tag in value['asset'].tags">
<a href="${value['asset'].blog.url}/tags/${tag}" class="link tag">${tag}</a>
            </li>
         </ul>
<ul class="tags admin" py:if="tg.config('identity.on', False) and not tg.identity.anonymous">
            <li py:for="tag in value['asset'].tags">
               <a href="remove/${tag}" class="tag-del">
                  <img src="/static/img/icons/tag_blue_delete.png" />
               </a>
<a href="${value['asset'].blog.url}/tags/${tag}" class="link tag">${tag}</a>
            </li>
<li class="add"><a href="#" class="link tag-add">new tag</a></li>
         </ul>
      </div>
   """

A list of active portlets is passed to each template call (I use a registered global in tg.portlets, but they can be passed as arguments, too). The template calls the portlets like this:

<div py:for="portlet in hasattr(self, 'portlets') and portlets or tg.portlets()" py:replace="portlet(self.__dict__)" />

self.__dict__ is passed to allow the portlet full access to all arguments passed to the template.

Matthew Bevan, Systems Administrator
Top Floor Computer Systems Ltd.


Attachment: smime.p7s
Description: S/MIME cryptographic signature

Reply via email to