[Twisted-web] Page object design: wrapping a dictionary containing data

Matt Goodall matt at pollenation.net
Sun Jan 23 03:27:23 MST 2005

On Sun, 2005-01-23 at 20:12 +1100, Mary Gardiner wrote:
> I've got a Page object that derives all its data from a dictionary, eg:
> {
>     'project_name': 'project',
>     'project_description': 'hello',
>     # ... you get the idea
> }
> What's a good way for the Page object to wrap that dictionary that
> doesn't involve doing this:
>     def data_name(self, ctx, data):
>         return self.d['project_name']
>     def data_description(self, ctx, data):
>         return self.d['project_description']
>     # and so on for every key in the dictionary
> I vaguely recall that the equivalent of this used to be possible in
> Woven:
>     def data_project(self, ctx, data):
>         return self.d
> and then that the data items were accessible by (say)
> stan.directive("project/project_name"). This doesn't look like its
> supported in Nevow. Is this correct? What alternative designs are there
> for something like this?

If a dict is the currently in scope data (the IData) for a template then
nested data directives will look inside the dict (see
accessors.DictionaryContainer for details):

        <div n:data="personAsDict">
          <p n:data="name">...</p>
          <p n:data="address">...</p>

In that example, the 'name' and 'address' directives are equivalent to
personAsDict['name'] and personAsDict['address'] respectively.

There's also the mapping render (rend.mapping) That fills slots with the
content of the dict:

        <div n:data="personAsDict" n:render="mapping">
          <p><n:slot name="name" /></p>
          <p><n:slot name="address" /></p>

Hope this helps.

Cheers, Matt

More information about the Twisted-web mailing list