File: objcontainer.py

package info (click to toggle)
nevow 0.9.31-3
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 2,804 kB
  • ctags: 4,921
  • sloc: python: 19,639; ansic: 136; sh: 53; xml: 42; makefile: 25; sql: 5
file content (55 lines) | stat: -rw-r--r-- 1,667 bytes parent folder | download | duplicates (7)
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
"""Example of using nevow.accessors.ObjectContainer to allow data directives to
look inside application types.
"""

from nevow import accessors, inevow, loaders, rend, tags as T
from twisted.python.components import registerAdapter

class Image:
    """An image consisting of a filename and some comments.
    """
    def __init__(self, filename, comments):
        self.filename = filename
        self.comments = comments
        
        
# Register the adapter so Nevow can access Image instance attributes.
registerAdapter(accessors.ObjectContainer, Image, inevow.IContainer)


# Just a list of images to render in the page.
images = [
    Image('python.gif', ['Hisssssssss']),
    Image('cat.gif', ['Meeow', 'Purrrrrrrr']),
    ]
    
    
class ImagePage(rend.Page):
    """A simple page that renders a list of images. We registered an adapter
    earlier so that the data= directives inside the pattern can look inside
    Image instances.
    """
    
    addSlash = True
    
    def render_images(self, ctx, data):
        """Render a list of images.
        """
        tag = T.div(data=images, render=rend.sequence)[
            T.div(pattern='item')[
                T.p(data=T.directive('filename'), render=T.directive('data')),
                T.ul(data=T.directive('comments'), render=rend.sequence)[
                    T.li(pattern='item', render=T.directive('data')),
                    ],
                ],
            ]
        return tag
        
    docFactory = loaders.stan( T.html[T.body[T.directive('images')]] )
        
        
def createResource():
    """Create the root resource of the example.
    """
    return ImagePage()