Warning, big image!!!!
Whereas, ’special forces hand signals’ was the number one search on dotnot.org from google, and whereas the link on my prior post has disappeared, I have decided to serve the image directly from here: special forces hand signals
Popularity: 27%
…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.
- Set up the imports (1-4)
- define the handler for mod_python to kick in (6)
- (Optional) create a session and add it to the request (8-9)
- tell the client the result will be HTML (10)
- adjust the uri down to a template filename. For instance if the request uri was ‘/example/test.tmpl’, then strip off the ‘/example/’ to come up with ‘test.tmpl’. Note that line 11 could just as easily read
base_path = "/example/" (11,12)
- Find the template in the path (this code is found here) (13)
- construct the template, passing in the request object as part of the template’s available namespace. For example, in mod_python, you can get the path of the request uri by
req.uri. In the template, you just use $uri, as the req. is silent
(14)
- ‘fill’ the template (execute it?), returning the string result (15)
- finish the request and return success (16-19)
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: 13%
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:
- php.net’s awesome documentation
- 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%
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: 13%
Found a house in Rough and Ready, CA (just west of Nevada City/Grass Valley), made an offer, and the offer has been accepted. Now we aren’t as homeless as we used to be
The house is 2600 square feet on 5.3 acres, with a 2400 square foot shop. The shop is a bit small, but I’ll learn to make do
The house is 4 bedrooms and 2.5 baths, with a 3 car garage. We are far enough ‘in the country’ to have a well and septic system. It is 10 months old (which is jolly good). I will try and post pictures soon.
When this is all said and done, I will also have a recommendation for a realtor in the Nevada County, CA area. He has been great so far, we just need to finish so that I can share the story.
Popularity: 32%