This file is indexed.

/usr/share/pyshared/zope/viewlet/communicating-viewlets.txt is in python-zope.viewlet 3.7.2-0ubuntu4.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
==============================================
A technique for communication between viewlets
==============================================


Sometimes one wants viewlets to communicate with each other to supplement a
more generic viewlet's behaviour with another viewlet. One example would be a
viewlet that contains a search form created by a library such as z3c.form,
plus a second viewlet that provides a list of search results.

This is very simple to accomplish with zope.viewlet but has turned out not to
be obvious, so here is an explicit example. It is not written as a doc test
since it uses z3c.form which should not become a dependency of zope.viewlet.


The viewlets
============

For the purpose of this example, we simulate a search form. Our search results
are simply the characters of the search term and are stored on the viewlet as
an attribute:


    class ISearchForm(zope.interface.Interface):

        searchterm = zope.schema.TextLine(title=u"Search term")


    class SearchForm(z3c.form.form.Form):

        ignoreContext = True

        fields = z3c.form.field.Fields(ISearchForm)

        results = ""

        @z3c.form.button.buttonAndHandler(u"Search")
        def search(self, action):
            data, errors = self.extractData()
            self.results = list(data["searchterm"])


(Notice that this example is minimized to point out communication between
viewlets, and no care is taken to handle the form itself in the best way
possible. In particular, one will probably want to make sure that the actual
search is not performed more than once, which may happen with the above code.)

The result list viewlet needs to display the results stored on the search
form. Therefore, the result list viewlet needs to access that viewlet, which
is probably the only tricky part (if there is any at all) in this example:
Since viewlets know their viewlet manager which lets viewlets be looked up by
ID, it is all a matter of the result list viewlet knowing the ID of the search
form.

We'll store the search form viewlet's ID as an attribute of the result list
viewlet. Let's hard-code the ID for a start, then the result list looks like
this:


    class ResultList(zope.viewlet.viewlet.ViewletBase):

        searchform_id = "searchform"

        def update(self):
            super(ResultList, self).update()
            searchform = self.manager[self.searchform_id]
            searchform.update()
            self.results = searchform.results

        def render(self):
            return "<ul>%s</ul>" % "\n".join(u"<li>%s</li>" % x
                                             for x in self.results)


Registering the viewlets
========================

As long as we treat the ID of the search form as hard-coded, we have to use
the correct name when registering the two viewlets:


    <viewlet
       name="searchform"
       class="...SearchForm"
       permission="zope.Public"
       />

    <viewlet
       name="resultlist"
       class="...ResultList"
       permission="zope.Public"
       />


Making the ID of the search form more flexible now doesn't even require
changing any code: the viewlet directive may be passed arbitrary attributes
which will be available as attributes of the ResultList objects. The attribute
that holds our search form's ID is searchform_id, so we might register the
viewlets like this:


    <viewlet
       name="sitesearch"
       class="...SearchForm"
       permission="zope.Public"
       />

    <viewlet
       name="resultlist"
       class="...ResultList"
       permission="zope.Public"
       searchform_id="sitesearch"
       />