[mod_python] Writing an Handler for this purpose ??

Jorey Bump list at joreybump.com
Wed Mar 12 11:33:20 EDT 2008


Nilesh Govindrajan wrote, at 03/12/2008 10:48 AM:
> Aah! Atleast you replied. Thanks a million for replying.
> 
> Now, you mean to say something like this -
> 
> Set handler mod_python for template files
> 
> --->
> 
> Write a Handler to parse the template in a variable.
> 
> --->
> 
> Print the variable using req.write()
> 
> ...
> 
> Am I right ?

Not exactly. Consider the template files a resource that you use in your 
code via KID. You won't need a handler that manipulates templates 
directly requested in a URL (theoretically, you may be able to create a 
PSP-like handler based on KID, but that seems to go against the purpose 
and philosophy of KID, as it would require your templates to contain a 
lot of programming logic to the point they wouldn't really be templates 
anymore).

So, imagine you're using mod_python's Publisher. Your page is hello.py, 
available via the URL:

  http://example.com/hello

The contents of hello.py might be:

import kid
def index(req):
     template = Template(file='test.kid', foo='Hello', baz='World')
     return template.serialize()

Or, if you write your own handler, it might look like this:

from mod_python import apache
import kid
def handler(req):
     template = Template(file='test.kid', foo='Hello', baz='World')
     req.content_type = "text/html"
     req.write(template.serialize())
     return apache.OK

This is an untested oversimplification that assumes you have KID 
importing correctly and that mod_python is properly configured.



More information about the Mod_python mailing list