[Twisted-web] really beginner example

Ellers andrew at ellerton.net
Mon Apr 5 12:24:05 MDT 2004

Hey Alex,
Thanks for your help - thats awesome.
More comments below...

On Sun, 04 Apr 2004 20:10:08 -0400, Alex Levy <mesozoic at polynode.com> wrote:

> On Sun, 2004-04-04 at 17:06, Ellers wrote:
>> Right now I want to use my own Site object, not a subclass of the
>> Nevow site object, and use the Nevow classes to generate html based on
>> templates from files.
> Why in particular don't you want to use NevowSite? You don't need to
> subclass NevowSite, it can be used as-is (see the attached example).

thats a good question.

Primarily my logic is that I shouldn't "have" to use a NevowSite. The twisted framework is, well, quite a heavy framework as it is. I have my existing (admittedly somewhat lame) resource.Resource subclasses, and I felt that I "should" be able to use Nevow without having to do everything its way (ie using NevowSite).

Hope that doesn't sound too militant... I guess I figure Nevow sounds like a great opportunity for a cool API, but I'm hoping I don't have to use it as a heavyweight framework. Where the difference between API and framework is how locked in I am to do my code "its way".


>> I have this in a file "hello_template.html":
>> (snip)
> I've attached a slight reworking of your example. You can run this
> normally, or you can run it with 'twistd -ony example1.tac.py' to
> actually start a web server. It shows both how to use data_ and render_
> methods, and also answers your question about rendering pages into plain
> text strings.
> Hope this helps.

it does - thanks!

The core of what you sent is this part:

def printPage():
     p = HelloPage()
     # Ordinarily this remembering nonsense is done for us.
     p.remember(p, inevow.IData)
     p.remember(p, inevow.IRendererFactory)
     # Rend.Page.renderString() returns a deferred, so...
     def gotRenderedPage(html):
         print 'Page:'
         print html

Nifty trick with the deferred and the gotRenderedPage() callback. I'm still having trouble getting my mind around deferred so thats helpful.

Can you expand a bit on how the remember() calls are helpful? I use interfaces a lot generally (though usually thats C++ or Java), but I don't quite get how they're useful in this case?

thanks for any tips :)


More information about the Twisted-web mailing list