[Twisted-web] nevow HTML based template fragment problem

Matt Goodall matt at pollenation.net
Fri Aug 13 09:43:26 MDT 2004

On Fri, 2004-08-13 at 10:42, Tazzo wrote:
> How can I insert a html rendered template inside another
> html rendered template?
> I mean, I have a rend.Page class with a docFactory that load
> a template (ex: page.html) with a header tag to renderize.
> The problem is that the header tag must be loaded from another
> rend.Page class who have is docFactory template (ex: header.html)
> to renderize, before send it. 
> How can I use the header rend.Page class from a render method in
> another rend.Page class?

Short answer ...

Return the header rend.Page from the page's render method.

Long answer ...

Assuming the header contains dynamic content then your header's class
should really extend rend.Fragment:

    class Header(rend.Fragment):
        def render_something(self, ctx, data):
            return something
        docFactory = loaders.xmlfile('header.html')

    class Page(rend.Page):
        def render_header(self, ctx, data):
            return Header()
        docFactory = loaders.xmlfile('page.html')

But, if the header is completely static then you can just return a

    class Page(rend.Page):
        def render_header(self, ctx, data):
            return loaders.xmlfile('header.html')
        docFactory = loaders.xmlfile('page.html')

Note: there's not actually a whole lot of difference between Fragment
and Page in this scenario but a fragment is conceptually cleaner.

Hope this helps.

Cheers, Matt

    /  \__     Matt Goodall, Pollenation Internet Ltd
    \__/  \    w: http://www.pollenation.net
  __/  \__/    e: matt at pollenation.net
 /  \__/  \    t: +44 (0)113 2252500
 \__/  \__/
 /  \          Any views expressed are my own and do not necessarily
 \__/          reflect the views of my employer.

More information about the Twisted-web mailing list