[Twisted-web] fragments with child lookup

Markus Schiltknecht markus at bluegap.ch
Mon Sep 25 08:49:27 CDT 2006


Valentino Volonghi aka Dialtone wrote:
> I don't completely understand why you really really really need this but...

I see... unfortunately I don't know how to explain this better. I don't
understand why writing a pastebin, a blog, or a news system should not
be written as a component / Fragment, which can be embedded into other
sites. And by embedded I mean URL _and_ template embedding. Isn't that
obvious to give a lot of flexibility?

> def render_content(self, level):
>    def _(ctx, data):
>        try:
>            fragment_name = inevow.ICurrentSegments(ctx)[level]
>        ...

Hm... looks tricky. I didn't know about def _(...). But who is expected
to pass the level to the renderer? The designer?

> Used:
> <nevow:invisible nevow:render="content 1">...</nevow:invisible>

That way I still have the level hard coded into the template.
Unfortunately, that's not an option for me. The designer should be able
to 'drag' the components around without having to worry about how deep
it's templates are nested.

>> Sill I'm still surprised, nevow does not provide that functionality.
> It's a bit frustrating for me to keep answering while you consistently say
> that 'nevow does not provide XXX functionality', can you please stop 
> implying that nevow is a pile of junk compared to others?

Sorry, I didn't mean to put nevow down. Not at all. I'm quite impressed
by how clever and thought through a templating system can be. And I
would not invest that much time into figuring out how to do what I want
if I didn't believe in it being mostly superior to all others.

>> I think another way to look at my problem is: how do I code a pastebin 
>> that _can_ be embedded like that? A pastebin which can be a fragment 
>> of another page.
> Using macros of course and it's very easy to do, and the code I showed 
> you already does that and no it's not too static because you can make 
> anything that looks static a lot more dynamic, 

Uh.. macros? This is where it gets frustrating for me, because I think 
you still don't known what I want to achieve. Macros don't know a lot. 
Their context is very limited. How am I supposed to resolve the all the 
locateChild machinery? How do I give that information to the 'child', 
the pastebin in my example?

I've tried using a macro, see the code below. With some of my fragment 
code (the dictionary thing, as you said that would be the right way to 
do it). That portion of the code works, but with the macro I don't know 
how pass necessary information around.

As it is done below, only the pastebin's RootPage is displayed. I don't 
know how to make subsites of it work. I.e.:

localhost:8080/            -> okay, renders the _default fragment
localhost:8080/pastebin/   -> okay, renders the pastebin's RootPage
                               embedded in the SuperRootPage's docFactory
localhost:8080/pastebin/1/ -> not okay, still renders the pastebin's

I'm not going to say 'nevow can't do it' anymore, but if it really can, 
I would love to learn _how_ to do it.

Please excuse, but as long as I'm unconvinced of that capability, I have 
a good reason to believe nevow has not implemented that feature, yet. 
That's okay, however, and I believe I should keep trying to make nevow 
do what I want it to do.

> this is python not C++.

Hm... check 'C++ template metaprogramming'  *joking*
How static is that?   :-)

Anyway, once again, thank you for your patience.



My macro try:

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,33 @@
  pastebin = FSPasteBinService('data')

-appResource = pages.RootPage(pastebin)
+class SuperRootPage(rend.Page):
+    docFactory = loaders.stan(
+        T.div[
+            T.h1['hello world'],
+            T.invisible(render=T.directive('child_content'))
+        ])
+    fragments = {
+        '_default': loaders.stan(T.div[T.h2['hello component based 
+        'pastebin': 
+    }
+    def render_child_content(self, ctx, data):
+        try:
+            fragment_name = inevow.ICurrentSegments(ctx)[0]
+            return self.fragments[fragment_name]
+        except KeyError:
+            return self.fragments['_default']
+    def macro_child_content(self, ctx):
+        return pages.RootPage(pastebin)
+    def locateChild(self, ctx, segments):
+        return self, ()
+appResource = SuperRootPage()
  appResource.putChild('robots.txt', static.File('static/robots.txt'))
  vResource = vhost.VHostMonsterResource()
  appResource.putChild('vhost', vResource)

More information about the Twisted-web mailing list