2.1 An example

Our first template follows a long noble tradition in computer tutorials. It produces a familiar, friendly greeting. Here's the template:

Hello, world!

... the output:

Hello, world!

... and the .py template module cheetah-compile produced, with line numbers added:

  1	#!/usr/bin/env python
    
  2	"""
  3	Autogenerated by CHEETAH: The Python-Powered Template Engine
  4	 CHEETAH VERSION: 0.9.12
  5	 Generation time: Sat Apr 20 14:27:47 2002
  6	   Source file: x.tmpl
  7	   Source file last modified: Wed Apr 17 22:10:59 2002
  8	"""
    
  9	__CHEETAH_genTime__ = 'Sat Apr 20 14:27:47 2002'
 10	__CHEETAH_src__ = 'x.tmpl'
 11	__CHEETAH_version__ = '0.9.12'
    
 12	##################################################
 13	## DEPENDENCIES
    
 14	import sys
 15	import os
 16	import os.path
 17	from os.path import getmtime, exists
 18	import time
 19	import types
 20	from Cheetah.Template import Template
 21	from Cheetah.DummyTransaction import DummyTransaction
 22	from Cheetah.NameMapper import NotFound, valueForName, 
           valueFromSearchList
 23	import Cheetah.Filters as Filters
 24	import Cheetah.ErrorCatchers as ErrorCatchers
    
 25	##################################################
 26	## MODULE CONSTANTS
    
 27	try:
 28	    True, False
 29	except NameError:
 30	    True, False = (1==1), (1==0)
    
 31	##################################################
 32	## CLASSES
    
 33	class x(Template):
 34	    """
 35	    
 36	    Autogenerated by CHEETAH: The Python-Powered Template Engine
 37	    """
 38	    ##################################################
 39	    ## GENERATED METHODS
    
    
 40	    def __init__(self, *args, **KWs):
 41	        """
 42	        
 43	        """
    
 44	        Template.__init__(self, *args, **KWs)
 45	        self._filePath = 'x.tmpl'
 46	        self._fileMtime = 1019106659
    
 47	    def respond(self,
 48	            trans=None,
 49	            dummyTrans=False,
 50	            VFS=valueFromSearchList,
 51	            VFN=valueForName,
 52	            getmtime=getmtime,
 53	            currentTime=time.time):
    
    
 54	        """
 55	        This is the main method generated by Cheetah
 56	        """
    
 57	        if not trans:
 58	            trans = DummyTransaction()
 59	            dummyTrans = True
 60	        write = trans.response().write
 61	        SL = self._searchList
 62	        filter = self._currentFilter
 63	        globalSetVars = self._globalSetVars
 64	        
 65	        ########################################
 66	        ## START - generated method body
 67	        
 68	        if exists(self._filePath) and getmtime(self._filePath) > \
                    self._fileMtime:
 69	            self.compile(file=self._filePath)
 70	            write(getattr(self, self._mainCheetahMethod_for_x)
                        (trans=trans))
 71	            if dummyTrans:
 72	                return trans.response().getvalue()
 73	            else:
 74	                return ""
 75	        write('Hello, world!\n')
 76	        
 77	        ########################################
 78	        ## END - generated method body
 79	        
 80	        if dummyTrans:
 81	            return trans.response().getvalue()
 82	        else:
 83	            return ""
 84	        
 85	    ##################################################
 86	    ## GENERATED ATTRIBUTES
    
    
 87	    __str__ = respond
    
 88	    _mainCheetahMethod_for_x= 'respond'
    
    
 89	# CHEETAH was developed by Tavis Rudd, Chuck Esterbrook, Ian Bicking 
        #     and Mike Orr;
 90	# with code, advice and input from many other volunteers.
 91	# For more information visit http://www.CheetahTemplate.org
    
 92	##################################################
 93	## if run from command line:
 94	if __name__ == '__main__':
 95	    x().runAsMainProgram()

(I added the line numbers for this Guide, and split a few lines to fit the page width. The continuation lines don't have line numbers, and I added indentation, backslashes and '#' as necessary to make the result a valid Python program.)

The examples were generated from CVS versions of Cheetah between 0.9.12 and 0.9.14.