[Twisted-web] fragments with child lookup

Markus Schiltknecht markus at bluegap.ch
Sat Sep 23 07:52:25 CDT 2006


Hi again,

I've now had a look at your source code:

Valentino Volonghi aka Dialtone wrote:
> http://hg.stiq.it/stiq for some more code.

I've learned about nevow macros. I don't quite understand what the big 
difference to a renderer is, but that's another story...

I'm still trying to get my 'component' based thinking migrated to nevow. 
As an exercise, I thought I'd try to use the nevow example pastebin as a 
component. What I want to achieve is having the pastebin available under 
http://localhost:8080/pastebin/, wrapped within my own template.
http://localhost:8080/  should simply show a welcome page, within the 
same template.

For this experiment, I've simply stuffed a SuperiorRootPage class into 
pastebin.tac. The simplistic template is included as stan. The pastebin 
should be rendered inside that (so, my 'hello world' title would appear 
on top). Of course, the main pastebin template (templates/site.html) 
would need to be adjusted. But not the python code.

I don't know if I should think of the included pastebin as a Fragment? 
Certainly it's a Page, as it is.

Bellow is what I've tried. I still don't know how to make such a thing 
work. Somebody care to enlighten me?

Regards

Markus


Index: pastebin.tac
===================================================================
--- pastebin.tac        (revision 9132)
+++ pastebin.tac        (working copy)
@@ -5,6 +5,7 @@

  from nevow import appserver
  from nevow import vhost
+from nevow import inevow, rend, stan, loaders, tags as T

  from pastebin import interfaces
  from pastebin.service import FSPasteBinService
@@ -16,7 +17,31 @@
  pastebin = FSPasteBinService('data')
  pastebin.setServiceParent(application)

-appResource = pages.RootPage(pastebin)
+class SuperiorRootPage(rend.Page):
+    addSlash = True
+    docFactory = loaders.stan(
+        T.html[
+            T.head(),
+            T.body[
+                T.h1['hello world'],
+                T.invisible(macro=T.directive('content')),
+            ]])
+
+    def locateChild(self, ctx, segments):
+        self.remaining_segments = segments[1:]
+        return self, ()
+
+    child_pastebin = pages.RootPage(pastebin)
+
+    def macro_content(self, ctx):
+        print "remaining_segments: %s" % str(self.remaining_segments)
+        if len(self.remaining_segments) == 0:
+            return "welcome!"
+        else:
+            # what to return here???
+            return "duh!"
+
+appResource = SuperiorRootPage(pastebin)
  appResource.putChild('robots.txt', static.File('static/robots.txt'))
  vResource = vhost.VHostMonsterResource()
  appResource.putChild('vhost', vResource)




More information about the Twisted-web mailing list