[Twisted-web] fragments with child lookup

Markus Schiltknecht markus at bluegap.ch
Mon Sep 25 02:32:59 CDT 2006


Hi Valentino,

thank you for your patience.

Valentino Volonghi aka Dialtone wrote:
> I think you are trying to use nevow in the same way you use other 
> frameworks which unfortunately doesn't work at all.

Yeah, I think that's my problem.

Thus I've also tried to do it as you suggest: using a dictionary of 
Fragm... ehm.... Pages. Anyway, this seems to be how a lot of samples 
are built.

It also shows, what I would like to have simplified: I don't want to 
write 'render_content_xy' methods for every level I'm introducing. In 
fact, I'd like to write a ContainerPage, which reads it's content 
entirely from a directory, including what templates to show in which 
order (You will note, that calling www.mysite.com/kontakt/info returns a 
ContactInfoPage, which renders the BasePage.docFactory 
(templates/common.html), then includes fragment 1, and finally fragment 
2 - this is a hierarchy, IMHO).

Regards

Markus



Here is what I think is more nevow like:


from nevow import rend, loaders, tags as T, inevow, static

class BasePage(rend.Page):
     addSlash = True
     docFactory = loaders.xmlfile('templates/common.html')

     fragments_1 = {
         '_default': loaders.xmlfile('templates/default.html', 
ignoreDocType=True),
         'kontakt': loaders.stan(T.span(render=T.directive('content_2'))),
     }

     def render_title(self, ctx, data):
         ctx.fillSlots('title', 'fmt website')
         return ctx

     def render_content_1(self, ctx, data):
         try:
             fragment_name = inevow.ICurrentSegments(ctx)[0]
             ctx.fillSlots('content', self.fragments_1[fragment_name])
         except KeyError:
             ctx.fillSlots('content', self.fragments_1['_default'])
         return ctx

     def render_content_2(self, ctx, data):
         try:
             fragment_name = inevow.ICurrentSegments(ctx)[1]
             ctx.fillSlots('content', self.fragments_2[fragment_name])
         except KeyError:
             ctx.fillSlots('content', self.fragments_2['_default'])
         return ctx

class ContactInfoPage(BasePage):
     pass

class ContactPage(BasePage):
     child_info = ContactInfoPage

class RootPage(BasePage):
     child_kontakt = ContactPage
     child_layout = static.File('layout')




More information about the Twisted-web mailing list