14.2.2 The Inheritance Approach

In the Inheritance approach, your template object doubles as as Webware servlet, thus these are sometimes called template-servlets. cheetah compile (section 4.2) automatically creates modules containing valid Webware servlets. A servlet is a subclass of Webware's WebKit.HTTPServlet class, contained in a module with the same name as the servlet. WebKit uses the request URL to find the module, and then instantiates the servlet/template. The servlet must have a .respond() method (or .respondToGet(), .respondToPut(), etc., but the Cheetah default is .respond()). Servlets created by cheetah compile meet all these requirements.

(Cheetah has a Webware plugin that automatically converts a .tmpl servlet file into a .py servlet file when the .tmpl servlet file is requested by a browser. However, that plugin is currently unavailable because it's being redesigned. For now, use cheetah compile instead.)

What about logic code? Cheetah promises to keep content (the placeholder values), graphic design (the template definition and is display logic), and algorithmic logic (complex calculations and side effects) separate. How? Where do you do form processing?

The answer is that your template class can inherit from a pure Python class containing the analytical logic. You can either use the #extends directive in Cheetah to indicate the superclass(es), or write a Python class statement to do the same thing. See the template Cheetah.Templates.SkeletonPage.tmpl and its pure Python class Cheetah.Templates._SkeletonPage.py for an example of a template inheriting logic code. (See sections 8.2 and 8.3 for more information about #extends and #implements. They have to be used a certain right way.)

If #WebKit.HTTPServlet is not available, Cheetah fakes it with a dummy class to satisfy the dependency. This allows servlets to be tested on the command line even on systems where Webware is not installed. This works only with servlets that don't call back into WebKit for information about the current web transaction, since there is no web transaction. Trying to access form input, for instance, will raise an exception because it depends on a live web request object, and in the dummy class the request object is None.

Because Webware servlets must be valid Python modules, and ``cheetah compile'' can produce only valid module names, if you're converting an existing site that has .html filenames with hyphens (-), extra dots (.), etc, you'll have to rename them (and possibly use redirects).