Tuesday, March 07, 2006

 

Basic FOR loops

.tmpl file is:

Content-type: text/html

<html>
<h2>You sent signal=$signalval</h2>
#for $item in $l_list:
$item <br>
#end for
</html>

cheetah_test.cgi is:

#!/usr/bin/python

from Cheetah.Template import Template
import cgi

# Gather the entries from the form.
try:
The_Form = cgi.FieldStorage()
except:
print "cgi error"
sys.exit()

try:
signal = The_Form['signal'].value
except:
# If user doesn't supply a signal value, set to 'start'.
signal = 'start'

l = [1,2,3,4,5,10]
mydict = {'signalval': signal,
'l_list': l}
t = Template(file='cheetah_test.tmpl', searchList=[mydict])
print t

 

Basic Cheetah

Basic template file, foo.tmpl:

see cheetah_notes.txt

Basic use of a template:

tcs = ['runquery', 'executequery', 'deletequery']
mp = {"classundertest":"query_runner", "testcases":tcs}

from Cheetah.Template import Template
t = Template(file="default_pyunit.tmpl", searchList=[mp])
print t

===
From http://www-128.ibm.com/developerworks/opensource/library/os-pythcheetah/?ca=dgr-lnxw06PythonCheetah

from Cheetah.Template import Template
from DummyObjects import dummyUser, dummyOrder
definition = """Hello, $user.firstName.

Your order (#$order.id) has shipped:"""
print Template(definition, searchList=[{'user' : dummyUser,
'order' : dummyOrder}])

 

Installed Cheetah templating module

The README file has info on installing for a single user (instead of site-wide install).

Download and tar zxvf it to tmp subdir.
cd to the subdir

[root@myth Cheetah-1.0]# python ./setup.py install
running install
running build
running build_py
creating build
creating build/lib.linux-i686-2.3
creating build/lib.linux-i686-2.3/Cheetah
copying src/Version.py -> build/lib.linux-i686-2.3/Cheetah
copying src/__init__.py -> build/lib.linux-i686-2.3/Cheetah
copying src/DummyTransaction.py -> build/lib.linux-i686-2.3/Cheetah
copying src/TemplateCmdLineIface.py -> build/lib.linux-i686-2.3/Cheetah
copying src/ImportManager.py -> build/lib.linux-i686-2.3/Cheetah
copying src/CheetahWrapper.py -> build/lib.linux-i686-2.3/Cheetah
copying src/CacheRegion.py -> build/lib.linux-i686-2.3/Cheetah
copying src/SettingsManager.py -> build/lib.linux-i686-2.3/Cheetah
copying src/NameMapper.py -> build/lib.linux-i686-2.3/Cheetah
copying src/FileUtils.py -> build/lib.linux-i686-2.3/Cheetah
copying src/Compiler.py -> build/lib.linux-i686-2.3/Cheetah
copying src/ImportHooks.py -> build/lib.linux-i686-2.3/Cheetah
copying src/Template.py -> build/lib.linux-i686-2.3/Cheetah
copying src/ErrorCatchers.py -> build/lib.linux-i686-2.3/Cheetah
copying src/SourceReader.py -> build/lib.linux-i686-2.3/Cheetah
copying src/Parser.py -> build/lib.linux-i686-2.3/Cheetah
copying src/Servlet.py -> build/lib.linux-i686-2.3/Cheetah
copying src/Filters.py -> build/lib.linux-i686-2.3/Cheetah
creating build/lib.linux-i686-2.3/Cheetah/Templates
copying src/Templates/__init__.py -> build/lib.linux-i686-2.3/Cheetah/Templates
copying src/Templates/SkeletonPage.py -> build/lib.linux-i686-2.3/Cheetah/Templates
copying src/Templates/_SkeletonPage.py -> build/lib.linux-i686-2.3/Cheetah/Templates
creating build/lib.linux-i686-2.3/Cheetah/Tests
copying src/Tests/__init__.py -> build/lib.linux-i686-2.3/Cheetah/Tests
copying src/Tests/SyntaxAndOutput.py -> build/lib.linux-i686-2.3/Cheetah/Tests
copying src/Tests/CheetahWrapper.py -> build/lib.linux-i686-2.3/Cheetah/Tests
copying src/Tests/unittest_local_copy.py -> build/lib.linux-i686-2.3/Cheetah/Tests
copying src/Tests/Test.py -> build/lib.linux-i686-2.3/Cheetah/Tests
copying src/Tests/NameMapper.py -> build/lib.linux-i686-2.3/Cheetah/Tests
copying src/Tests/Template.py -> build/lib.linux-i686-2.3/Cheetah/Tests
copying src/Tests/FileRefresh.py -> build/lib.linux-i686-2.3/Cheetah/Tests
creating build/lib.linux-i686-2.3/Cheetah/Tools
copying src/Tools/__init__.py -> build/lib.linux-i686-2.3/Cheetah/Tools
copying src/Tools/MondoReport.py -> build/lib.linux-i686-2.3/Cheetah/Tools
copying src/Tools/RecursiveNull.py -> build/lib.linux-i686-2.3/Cheetah/Tools
copying src/Tools/SiteHierarchy.py -> build/lib.linux-i686-2.3/Cheetah/Tools
copying src/Tools/CGITemplate.py -> build/lib.linux-i686-2.3/Cheetah/Tools
creating build/lib.linux-i686-2.3/Cheetah/Utils
copying src/Utils/__init__.py -> build/lib.linux-i686-2.3/Cheetah/Utils
copying src/Utils/VerifyType.py -> build/lib.linux-i686-2.3/Cheetah/Utils
copying src/Utils/Indenter.py -> build/lib.linux-i686-2.3/Cheetah/Utils
copying src/Utils/Misc.py -> build/lib.linux-i686-2.3/Cheetah/Utils
copying src/Utils/WebInputMixin.py -> build/lib.linux-i686-2.3/Cheetah/Utils
creating build/lib.linux-i686-2.3/Cheetah/Utils/optik
copying src/Utils/optik/__init__.py -> build/lib.linux-i686-2.3/Cheetah/Utils/optik
copying src/Utils/optik/option_parser.py -> build/lib.linux-i686-2.3/Cheetah/Utils/optik
copying src/Utils/optik/option.py -> build/lib.linux-i686-2.3/Cheetah/Utils/optik
copying src/Utils/optik/errors.py -> build/lib.linux-i686-2.3/Cheetah/Utils/optik
running build_ext
building 'Cheetah._namemapper' extension
creating build/temp.linux-i686-2.3
creating build/temp.linux-i686-2.3/src
gcc -pthread -fno-strict-aliasing -DNDEBUG -O2 -g -pipe -m32 -march=i386 -mtune=pentium4 -D_GNU_SOURCE -fPIC -fPIC -I/usr/include/python2.3 -c src/_namemapper.c -o build/temp.linux-i686-2.3/src/_namemapper.o
gcc -pthread -shared build/temp.linux-i686-2.3/src/_namemapper.o -o build/lib.linux-i686-2.3/Cheetah/_namemapper.so
running build_scripts
creating build/scripts-2.3
copying and adjusting bin/cheetah-compile -> build/scripts-2.3
copying and adjusting bin/cheetah -> build/scripts-2.3
changing mode of build/scripts-2.3/cheetah-compile from 644 to 755
changing mode of build/scripts-2.3/cheetah from 644 to 755
running install_lib
creating /usr/lib/python2.3/site-packages/Cheetah
copying build/lib.linux-i686-2.3/Cheetah/Version.py -> /usr/lib/python2.3/site-packages/Cheetah
copying build/lib.linux-i686-2.3/Cheetah/__init__.py -> /usr/lib/python2.3/site-packages/Cheetah
copying build/lib.linux-i686-2.3/Cheetah/DummyTransaction.py -> /usr/lib/python2.3/site-packages/Cheetah
creating /usr/lib/python2.3/site-packages/Cheetah/Templates
copying build/lib.linux-i686-2.3/Cheetah/Templates/__init__.py -> /usr/lib/python2.3/site-packages/Cheetah/Templates
copying build/lib.linux-i686-2.3/Cheetah/Templates/SkeletonPage.py -> /usr/lib/python2.3/site-packages/Cheetah/Templates
copying build/lib.linux-i686-2.3/Cheetah/Templates/_SkeletonPage.py -> /usr/lib/python2.3/site-packages/Cheetah/Templates
copying build/lib.linux-i686-2.3/Cheetah/TemplateCmdLineIface.py -> /usr/lib/python2.3/site-packages/Cheetah
copying build/lib.linux-i686-2.3/Cheetah/_namemapper.so -> /usr/lib/python2.3/site-packages/Cheetah
creating /usr/lib/python2.3/site-packages/Cheetah/Tools
copying build/lib.linux-i686-2.3/Cheetah/Tools/__init__.py -> /usr/lib/python2.3/site-packages/Cheetah/Tools
copying build/lib.linux-i686-2.3/Cheetah/Tools/MondoReport.py -> /usr/lib/python2.3/site-packages/Cheetah/Tools
copying build/lib.linux-i686-2.3/Cheetah/Tools/RecursiveNull.py -> /usr/lib/python2.3/site-packages/Cheetah/Tools
copying build/lib.linux-i686-2.3/Cheetah/Tools/SiteHierarchy.py -> /usr/lib/python2.3/site-packages/Cheetah/Tools
copying build/lib.linux-i686-2.3/Cheetah/Tools/CGITemplate.py -> /usr/lib/python2.3/site-packages/Cheetah/Tools
copying build/lib.linux-i686-2.3/Cheetah/ImportManager.py -> /usr/lib/python2.3/site-packages/Cheetah
copying build/lib.linux-i686-2.3/Cheetah/CheetahWrapper.py -> /usr/lib/python2.3/site-packages/Cheetah
copying build/lib.linux-i686-2.3/Cheetah/CacheRegion.py -> /usr/lib/python2.3/site-packages/Cheetah
copying build/lib.linux-i686-2.3/Cheetah/SettingsManager.py -> /usr/lib/python2.3/site-packages/Cheetah
creating /usr/lib/python2.3/site-packages/Cheetah/Tests
copying build/lib.linux-i686-2.3/Cheetah/Tests/__init__.py -> /usr/lib/python2.3/site-packages/Cheetah/Tests
copying build/lib.linux-i686-2.3/Cheetah/Tests/SyntaxAndOutput.py -> /usr/lib/python2.3/site-packages/Cheetah/Tests
copying build/lib.linux-i686-2.3/Cheetah/Tests/CheetahWrapper.py -> /usr/lib/python2.3/site-packages/Cheetah/Tests
copying build/lib.linux-i686-2.3/Cheetah/Tests/unittest_local_copy.py -> /usr/lib/python2.3/site-packages/Cheetah/Tests
copying build/lib.linux-i686-2.3/Cheetah/Tests/Test.py -> /usr/lib/python2.3/site-packages/Cheetah/Tests
copying build/lib.linux-i686-2.3/Cheetah/Tests/NameMapper.py -> /usr/lib/python2.3/site-packages/Cheetah/Tests
copying build/lib.linux-i686-2.3/Cheetah/Tests/Template.py -> /usr/lib/python2.3/site-packages/Cheetah/Tests
copying build/lib.linux-i686-2.3/Cheetah/Tests/FileRefresh.py -> /usr/lib/python2.3/site-packages/Cheetah/Tests
creating /usr/lib/python2.3/site-packages/Cheetah/Utils
copying build/lib.linux-i686-2.3/Cheetah/Utils/__init__.py -> /usr/lib/python2.3/site-packages/Cheetah/Utils
copying build/lib.linux-i686-2.3/Cheetah/Utils/VerifyType.py -> /usr/lib/python2.3/site-packages/Cheetah/Utils
copying build/lib.linux-i686-2.3/Cheetah/Utils/Indenter.py -> /usr/lib/python2.3/site-packages/Cheetah/Utils
copying build/lib.linux-i686-2.3/Cheetah/Utils/Misc.py -> /usr/lib/python2.3/site-packages/Cheetah/Utils
copying build/lib.linux-i686-2.3/Cheetah/Utils/WebInputMixin.py -> /usr/lib/python2.3/site-packages/Cheetah/Utils
creating /usr/lib/python2.3/site-packages/Cheetah/Utils/optik
copying build/lib.linux-i686-2.3/Cheetah/Utils/optik/__init__.py -> /usr/lib/python2.3/site-packages/Cheetah/Utils/optik
copying build/lib.linux-i686-2.3/Cheetah/Utils/optik/option_parser.py -> /usr/lib/python2.3/site-packages/Cheetah/Utils/optik
copying build/lib.linux-i686-2.3/Cheetah/Utils/optik/option.py -> /usr/lib/python2.3/site-packages/Cheetah/Utils/optik
copying build/lib.linux-i686-2.3/Cheetah/Utils/optik/errors.py -> /usr/lib/python2.3/site-packages/Cheetah/Utils/optik
copying build/lib.linux-i686-2.3/Cheetah/NameMapper.py -> /usr/lib/python2.3/site-packages/Cheetah
copying build/lib.linux-i686-2.3/Cheetah/FileUtils.py -> /usr/lib/python2.3/site-packages/Cheetah
copying build/lib.linux-i686-2.3/Cheetah/Compiler.py -> /usr/lib/python2.3/site-packages/Cheetah
copying build/lib.linux-i686-2.3/Cheetah/ImportHooks.py -> /usr/lib/python2.3/site-packages/Cheetah
copying build/lib.linux-i686-2.3/Cheetah/Template.py -> /usr/lib/python2.3/site-packages/Cheetah
copying build/lib.linux-i686-2.3/Cheetah/ErrorCatchers.py -> /usr/lib/python2.3/site-packages/Cheetah
copying build/lib.linux-i686-2.3/Cheetah/SourceReader.py -> /usr/lib/python2.3/site-packages/Cheetah
copying build/lib.linux-i686-2.3/Cheetah/Parser.py -> /usr/lib/python2.3/site-packages/Cheetah
copying build/lib.linux-i686-2.3/Cheetah/Servlet.py -> /usr/lib/python2.3/site-packages/Cheetah
copying build/lib.linux-i686-2.3/Cheetah/Filters.py -> /usr/lib/python2.3/site-packages/Cheetah
byte-compiling /usr/lib/python2.3/site-packages/Cheetah/Version.py to Version.pyc
byte-compiling /usr/lib/python2.3/site-packages/Cheetah/__init__.py to __init__.pyc
byte-compiling /usr/lib/python2.3/site-packages/Cheetah/DummyTransaction.py to DummyTransaction.pyc
byte-compiling /usr/lib/python2.3/site-packages/Cheetah/Templates/__init__.py to __init__.pyc
byte-compiling /usr/lib/python2.3/site-packages/Cheetah/Templates/SkeletonPage.py to SkeletonPage.pyc
byte-compiling /usr/lib/python2.3/site-packages/Cheetah/Templates/_SkeletonPage.py to _SkeletonPage.pyc
byte-compiling /usr/lib/python2.3/site-packages/Cheetah/TemplateCmdLineIface.py to TemplateCmdLineIface.pyc
byte-compiling /usr/lib/python2.3/site-packages/Cheetah/Tools/__init__.py to __init__.pyc
byte-compiling /usr/lib/python2.3/site-packages/Cheetah/Tools/MondoReport.py to MondoReport.pyc
byte-compiling /usr/lib/python2.3/site-packages/Cheetah/Tools/RecursiveNull.py to RecursiveNull.pyc
byte-compiling /usr/lib/python2.3/site-packages/Cheetah/Tools/SiteHierarchy.py to SiteHierarchy.pyc
byte-compiling /usr/lib/python2.3/site-packages/Cheetah/Tools/CGITemplate.py to CGITemplate.pyc
byte-compiling /usr/lib/python2.3/site-packages/Cheetah/ImportManager.py to ImportManager.pyc
byte-compiling /usr/lib/python2.3/site-packages/Cheetah/CheetahWrapper.py to CheetahWrapper.pyc
byte-compiling /usr/lib/python2.3/site-packages/Cheetah/CacheRegion.py to CacheRegion.pyc
byte-compiling /usr/lib/python2.3/site-packages/Cheetah/SettingsManager.py to SettingsManager.pyc
byte-compiling /usr/lib/python2.3/site-packages/Cheetah/Tests/__init__.py to __init__.pyc
byte-compiling /usr/lib/python2.3/site-packages/Cheetah/Tests/SyntaxAndOutput.py to SyntaxAndOutput.pyc
byte-compiling /usr/lib/python2.3/site-packages/Cheetah/Tests/CheetahWrapper.py to CheetahWrapper.pyc
byte-compiling /usr/lib/python2.3/site-packages/Cheetah/Tests/unittest_local_copy.py to unittest_local_copy.pyc
byte-compiling /usr/lib/python2.3/site-packages/Cheetah/Tests/Test.py to Test.pyc
byte-compiling /usr/lib/python2.3/site-packages/Cheetah/Tests/NameMapper.py to NameMapper.pyc
byte-compiling /usr/lib/python2.3/site-packages/Cheetah/Tests/Template.py to Template.pyc
byte-compiling /usr/lib/python2.3/site-packages/Cheetah/Tests/FileRefresh.py to FileRefresh.pyc
byte-compiling /usr/lib/python2.3/site-packages/Cheetah/Utils/__init__.py to __init__.pyc
byte-compiling /usr/lib/python2.3/site-packages/Cheetah/Utils/VerifyType.py to VerifyType.pyc
byte-compiling /usr/lib/python2.3/site-packages/Cheetah/Utils/Indenter.py to Indenter.pyc
byte-compiling /usr/lib/python2.3/site-packages/Cheetah/Utils/Misc.py to Misc.pyc
byte-compiling /usr/lib/python2.3/site-packages/Cheetah/Utils/WebInputMixin.py to WebInputMixin.pyc
byte-compiling /usr/lib/python2.3/site-packages/Cheetah/Utils/optik/__init__.py to __init__.pyc
byte-compiling /usr/lib/python2.3/site-packages/Cheetah/Utils/optik/option_parser.py to option_parser.pyc
byte-compiling /usr/lib/python2.3/site-packages/Cheetah/Utils/optik/option.py to option.pyc
byte-compiling /usr/lib/python2.3/site-packages/Cheetah/Utils/optik/errors.py to errors.pyc
byte-compiling /usr/lib/python2.3/site-packages/Cheetah/NameMapper.py to NameMapper.pyc
byte-compiling /usr/lib/python2.3/site-packages/Cheetah/FileUtils.py to FileUtils.pyc
byte-compiling /usr/lib/python2.3/site-packages/Cheetah/Compiler.py to Compiler.pyc
byte-compiling /usr/lib/python2.3/site-packages/Cheetah/ImportHooks.py to ImportHooks.pyc
byte-compiling /usr/lib/python2.3/site-packages/Cheetah/Template.py to Template.pyc
byte-compiling /usr/lib/python2.3/site-packages/Cheetah/ErrorCatchers.py to ErrorCatchers.pyc
byte-compiling /usr/lib/python2.3/site-packages/Cheetah/SourceReader.py to SourceReader.pyc
byte-compiling /usr/lib/python2.3/site-packages/Cheetah/Parser.py to Parser.pyc
byte-compiling /usr/lib/python2.3/site-packages/Cheetah/Servlet.py to Servlet.pyc
byte-compiling /usr/lib/python2.3/site-packages/Cheetah/Filters.py to Filters.pyc
running install_scripts
copying build/scripts-2.3/cheetah-compile -> /usr/bin
copying build/scripts-2.3/cheetah -> /usr/bin
changing mode of /usr/bin/cheetah-compile to 755
changing mode of /usr/bin/cheetah to 755
running install_data
copying src/Tools/MondoReportDoc.txt -> /usr/lib/python2.3/site-packages/Cheetah/Tools
copying src/Templates/SkeletonPage.tmpl -> /usr/lib/python2.3/site-packages/Cheetah/Templates

This page is powered by Blogger. Isn't yours?