[Twisted-web] fragments with child lookup

Valentino Volonghi aka Dialtone dialtone at divmod.com
Fri Sep 22 09:20:46 CDT 2006


On Fri, 22 Sep 2006 15:27:38 +0200, Markus Schiltknecht <markus at bluegap.ch> wrote:
>Right. I try in nevow terms: I want to write a Page, which dynamically 
>displays different Fragments depending on the URL. I wanted to use all the 
>'locateChild()' mechanism for that, but this seems not possible.

Ok but this should be accomplished in a different way IMHO. You should have a Fragments container somewhere and lookup stuff in there. It doesn't make too much sense to have fragment children IMHO.

>Since Pages are derived from Fragments, I didn't think about using Fragments 
>in the first place at all. (Of course, I'm 'using' Fragments, but hidden 
>from me through the Page class, which inherits from Fragments).

Well... Pages can be used in place of the Fragments but they will be considered as Fragments with no extra functionality (even if the code is there) thus it's better to stick with Fragments entirely for this usecase.

>As you can see in my example, I've written my own 'locateFragmentChild()' 
>method which does exactly what I'm describing above.

Well but you just need to lookup stuff here and there, if you need to include  different fragments in a particular fragment then I guess this locateFragmentChild is sort of useful although the name is not the best I can think of, simply locateFragment should be enough (but as I can see there's really no fixed hiararchy of Fragments and you shouldn't use the same mechanism used in locateChild, I'd rather go with a dictionary containing all of them because in the end that's what you are actually doing, including a fragment depending on the url segment).

>The use case is very simple and (IMHO) common: I have a top top level 
>template containing all the HTML headers and some common design elements. 
>Plus the top level navigation. Depending on the URL, different other 
>templates should be inserted (Fragments?). If you know Mason (perl) or 
>Myghty (python), I want to migrate from there. They have a structure like 
>that:

I have never used Myghty nor Mason but I do what you are describing everytime. I use macros and Page objects with a small function that looks up the template that should be included in the macro from an attribute value of the current Page instance.

http://hg.stiq.it/stiq for some more code.

>autohandler                     # the default template which gets
>                                 # rendered for every request
>
>dhandler                        # a default handler, called if no child
>                                 # is given in the URL, i.e.:
>                                 # www.mysite.com/
>
>foo.html                        # a normal site available at:
>                                 # www.mysite.com/foo.html
>                                 # it gets rendered as 'child' of the
>                                 # toplevel autohandler, thus it
>                                 # inherits it's methods and template.
>
>foo/autohandler                 # the template for subdir 'foo' on:
>                                 # www.mysite.com/foo/*
>
>foo/bar.html                    # a site in the subdirectory
>                                 # www.mysite.com/foo/bar.html
>                                 # For this, the toplevel autohandler is
>                                 # rendered first, then foo/autohandler
>                                 # and then foo/bar.html

I don't see a connection between this and the fragments though.

>This is very usefull when writing 'hierarchical' sites, i.e. where you can 
>navigate in a tree. What's the best way to do that in nevow?

There are many different ways of doing it depending on the exact usecase.

>Something else I'm very thankful about are the 'preprocessors'. It seems 
>they have (more or less) recently been added. Any chance I can pass them 
>arguments? Currently I simply insert a renderer in my preprocessor, but I'm 
>not sure if I can do everything with just inserting renderers (but probably 
>I can, I'll see).

Yes, they are a recent addition for a usecase in Quotient. You can pass arguments by using closures of course. But inserting renderers is a strage usecase, I've never needed it and pre-processors were added to change urls on the fly (I think for caching purposes) when loading templates, not for changing templates in that hard way. You might find some problems in doing that, but I've never tried.



More information about the Twisted-web mailing list