Saturday
6
Mar 2004

integrate cheetah and mod_python

(6:19 pm) Tags: [How do I...]

…and I do not want to precompile my templates. This one was pretty simple, as there is one recipe on the Cheetah Wiki about it. We start there and just modify it a bit. Here goes. First, we add the following somewhere in our apache config (I added it to my .htaccess):

Apache configuration for mod_python and cheetah
AddHandler mod_python | .tmpl
PythonHandler cheetah

That was easy, now we write the code for cheetah.py:

Cheetah integration code
01: import string
02: from mod_python import apache
03: from mod_python import Session
04: from Cheetah import Template
05:  
06: def handler(req):
07:  
08:     session = Session.Session(req)
09:     req.session = session
10:     req.content_type = “text/html”
11:     base_path = req.get_options()[’base-path’]
12:     template_name = string.replace(req.uri, base_path, “”)
13:     template_name = findfile(template_name)
14:     template = Template.Template(file=template_name, searchList=[req])
15:     content = template.respond()    
16:     req.send_http_header()
17:     req.write(content)
18:  
19:     return apache.OK

Pretty simple really.

How much easier can it be? Now you create the template (hello.tmpl):

Cheetah template code
#set $place = “World”
Hello $place

Now, when you hit http://localhost/example/hello.tmpl, you will see “Hello World”.

Popularity: 14%

Comments: (1)

Cheetah and mod_python

(6:06 pm) Tags: [Software]

I have now settled on using Cheetah along with mod_python and Apache 2 for my new new web framework, and I find myself only missing 2 things from the PHP world:

  1. php.net’s awesome documentation
  2. overriding variables in {include} directives in Smarty
.
I have posted a question to the cheetah discussion list, to see how I would go about telling my includes that variables have changed. We’ll see what I can come up with. It is the only clean way that I can think of to make sure that child includes know nothing of their parent (the way Alex taught me to code my PHP) :)

Popularity: 10%

Comments: (1)

find a file in python’s path

(5:59 pm) Tags: [How do I...]

I am building a simple web framework in Python, and I need everything to be inheritable and extinsible. This includes content. So, I am doing the brain dead thing and using Python’s sys.path to search for content that may not have been overridden in the current context. The code was hacked up from a version I found here, in the first comment. I will try to find the link and post it later. This version is greatly simplified.

find a file in Python’s sys.path
import os, sys
 
def findfile(path):
    ”"”Find the file named path in the sys.path.
    Returns the full path name if found, None if not found”"”
    for dirname in sys.path:
        possible = os.path.join(dirname, path)
        if os.path.isfile(possible):
            return possible
    return None

Popularity: 14%

Comments: (0)