1
from zope.interface import implements
3
from nevow import inevow
6
## How does a request come to the Page?
8
## or How to use Nevow without all the fancy automations
11
# This is a simple Root page object, the inevow.IResource interface
12
# tells us that it must implement 2 methods:
13
# locateChild and renderHTTP.
14
# locateChild is used to find children of the current page, it must return a
15
# tuple of (page, remaining_segments)
16
# if there is no page, and you want to display a 404 page, you will need to return
19
implements(inevow.IResource)
21
def locateChild(self, ctx, segments):
22
# This locateChild is 'stupid' since it can only work if the tree of
23
# pages is static. Anyway it will work for our simple example
25
# If the server is looking for the root page segments will be ('',)
26
# then renderHTTP will be called on self
28
elif segments[0] == 'foo':
29
# Now we received a request whose segments had in the first position
31
# like http://example.org/foo/baz/ -> ('foo', 'baz')
32
# after the page has been located we return it with the remaining segments
34
return self.foo, segments[1:]
38
def renderHTTP(self, ctx):
39
# When the server needs to return a response to the request it will call
40
# the renderHTTP method that will return a string of what needs to be sent.
41
return """<html><body>Hello world!<br />
42
<a href="./foo" id="foo">foo</a></body></html>
46
implements(inevow.IResource)
48
def locateChild(self, ctx, segments):
49
# segments is the remaining segments returned by the root locateChild
51
if segments[0] == 'baz':
52
return self.baz, segments[1:]
56
def renderHTTP(self, ctx):
57
return """<html><body><h1 id="heading">You are in Foo</h1>
58
<a href="./foo/baz" id="baz">baz</a></body></html>
62
implements(inevow.IResource)
63
def locateChild(self, ctx, segments):
65
def renderHTTP(self, ctx):
66
return '<html><body><h1 id="heading">You are in Baz</h1></body></html>'
68
# We are adding children to the pages.
69
# This could also happen inside the class.