[Twisted-web] Including templates in CGI

Donovan Preston dp at ulaluma.com
Mon Jun 13 17:50:02 MDT 2005


On Jun 11, 2005, at 1:37 AM, Cliff Wells wrote:

> Hi,
>
> I just started playing with Nevow yesterday, and I've got a question.

<snip>

That all looks pretty good; you have more Python code than your PHP  
code, but that's because you check for more and it has more  
capability. This is the whole point of nevow; if you need to do  
something custom, you write some python code. And it's easy to share  
between pages, too.

However, here are a few suggestions:

>         if os.path.exists(filename):
>             fragClass = rend.Fragment
>             fragClass.docFactory = loaders.xmlfile(filename,  
> ignoreDocType=True)

This is weird and unnecessary. Fragment takes a docFactory argument  
for a reason. So, do this instead:

frag = Fragment(docFactory=loaders.xmlfile(filename,  
ignoreDocType=True))

>             ctx.tag.fillSlots(ctx.tag.attributes.get('slot'),  
> fragClass())
>             return ctx.tag

This is also pretty weird. I'm not understanding what the extra slot  
is giving you. If it were me, I would just "return frag" here and  
dispense with the slot in the template as well as the slot-filling.

>         else:
>             return "Page not found."
>
> index.html:
> -----------
> <div id="content">
>   <nevow:invisible nevow:render="include" variable="page"  
> slot="content">
>     <nevow:slot name="content" />

See comment above, I think this slot is extraneous...

>   </nevow:invisible>
> </div>
>
> This allows me to either specify the included file by accessing a  
> GET/POST variable:
>
> <nevow:invisible nevow:render="include" variable="page"  
> slot="content">

Neat!

> or directly specify the template like:
>
> <nevow:invisible nevow:render="include" file="home" slot="content">

Cool.

> This all seems to work just fine.  However, now I'm curious if  
> there is a better/more direct way of achieving this.

If it works for you, it works. :-) Again, the point of Nevow is to  
make it really easy for you to write Python code that does what you  
want it to do.

dp




More information about the Twisted-web mailing list