~divmod-dev/divmod.org/trunk

« back to all changes in this revision

Viewing changes to Nevow/examples/objcontainer/objcontainer.py

  • Committer: Jean-Paul Calderone
  • Date: 2014-06-29 20:33:04 UTC
  • mfrom: (2749.1.1 remove-epsilon-1325289)
  • Revision ID: exarkun@twistedmatrix.com-20140629203304-gdkmbwl1suei4m97
mergeĀ lp:~exarkun/divmod.org/remove-epsilon-1325289

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
"""Example of using nevow.accessors.ObjectContainer to allow data directives to
2
 
look inside application types.
3
 
"""
4
 
 
5
 
from nevow import accessors, inevow, loaders, rend, tags as T
6
 
from twisted.python.components import registerAdapter
7
 
 
8
 
class Image:
9
 
    """An image consisting of a filename and some comments.
10
 
    """
11
 
    def __init__(self, filename, comments):
12
 
        self.filename = filename
13
 
        self.comments = comments
14
 
        
15
 
        
16
 
# Register the adapter so Nevow can access Image instance attributes.
17
 
registerAdapter(accessors.ObjectContainer, Image, inevow.IContainer)
18
 
 
19
 
 
20
 
# Just a list of images to render in the page.
21
 
images = [
22
 
    Image('python.gif', ['Hisssssssss']),
23
 
    Image('cat.gif', ['Meeow', 'Purrrrrrrr']),
24
 
    ]
25
 
    
26
 
    
27
 
class ImagePage(rend.Page):
28
 
    """A simple page that renders a list of images. We registered an adapter
29
 
    earlier so that the data= directives inside the pattern can look inside
30
 
    Image instances.
31
 
    """
32
 
    
33
 
    addSlash = True
34
 
    
35
 
    def render_images(self, ctx, data):
36
 
        """Render a list of images.
37
 
        """
38
 
        tag = T.div(data=images, render=rend.sequence)[
39
 
            T.div(pattern='item')[
40
 
                T.p(data=T.directive('filename'), render=T.directive('data')),
41
 
                T.ul(data=T.directive('comments'), render=rend.sequence)[
42
 
                    T.li(pattern='item', render=T.directive('data')),
43
 
                    ],
44
 
                ],
45
 
            ]
46
 
        return tag
47
 
        
48
 
    docFactory = loaders.stan( T.html[T.body[T.directive('images')]] )
49
 
        
50
 
        
51
 
def createResource():
52
 
    """Create the root resource of the example.
53
 
    """
54
 
    return ImagePage()
55