13.5 All methods
Here is a list of all the standard methods and attributes that can be accessed
from a placeholder. Some of them exist for you to call, others are mainly used
by Cheetah internally but you can call them if you wish, and others are only
for internal use by Cheetah or Webware. Do not use these method names in mixin
classes (#extends
, section 8.2) unless you
intend to override the standard method.
Variables with a star prefix (*) are frequently used in templates or in
pure Python classes.
- compile(source=None, file=None, moduleName=None,
mainMethodName='respond') Compile the template. Automatically called
by
.__init__
.
- generatedModuleCode() Return the module code the compiler
generated, or
None
if no compilation took place.
- generatedClassCode() Return the class code the compiler
generated, or
None
if no compilation took place.
- * searchList() Return a reference to the underlying search list.
(a list of objects). Use this to print out your searchList for debugging.
Modifying the returned list will affect your placeholder searches!
- * errorCatcher() Return a reference to the current error
catcher.
- * refreshCache(cacheKey=None) If 'cacheKey' is not
None
,
refresh that item in the cache. If None
, delete all items in the
cache so they will be recalculated the next time they are encountered.
- * shutdown() Break reference cycles before discarding a servlet.
- * getVar(varName, default=NoDefault, autoCall=True) Look up a
variable in the searchList. Same as
$varName
but allows you to
specify a default value and control whether autocalling occurs.
- * varExists(varName, autoCall=True)
- * getFileContents(path) Read the named file. If used as a
placeholder, inserts the file's contents in the output without
interpretation, like
#include raw
. If used in an expression,
returns the file's content (e.g., to assign it to a variable).
- runAsMainProgram() This is what happens if you run a
.py template module as a standalone program.
- nonNumericInputError Exception raised by
.webInput
.
- * webInput(...) Convenience method to access GET/POST variables
from a Webware servlet or CGI script, or Webware cookie or session
variables. See section 14.7 for usage information.
- setting(name, default=NoDefault) Get a compiler setting.
- hasSetting(name) Does this compiler setting exist?
- setSetting(name, value) Set setting 'name' to 'value'.
See
#compiler-settings
, section
11.2.
- settings() Return the underlying settings dictionary. (Warning:
modifying this dictionary will change Cheetah's behavior.)
- copySettings() Return a copy of the underlying settings
dictionary.
- deepcopySettings() Return a deep copy of the underlying settings
dictionary. See Python's
copy
module.
- updateSettings(newSettings, merge=True) Update Cheetah's
compiler settings from the 'newSettings' dictionary. If 'merge' is true,
update only the names in newSettings and leave the other names alone.
(The SettingsManager is smart enough to update nested dictionaries one
key at a time rather than overwriting the entire old dictionary.)
If 'merge' is false, delete all existing settings so that the new ones are
the only settings.
- updateSettingsFromPySrcStr(theString, merge=True) Same,
but pass a string of
name=value
pairs rather
than a dictionary, the same as you would provide in a
#compiler-settings
directive, section
11.2.
- updateSettingsFromPySrcFile(path, merge=True) Same, but
exec a Python source file and use the variables it contains as the new
settings. (e.g.,
cheetahVarStartToken = "@"
).
- updateSettingsFromConfigFile(path, **kw) Same, but get the new
settings from a text file in ConfigParser format (similar to Windows'
*.ini file format). See Python's
ConfigParser
module.
- updateSettingsFromConfigFileObj Same, but read the open file
object 'inFile' for the new settings.
- updateSettingsFromConfigStr(configStr, convert=True, merge=True
Same, but read the new settings from a string in ConfigParser format.
- writeConfigFile(path) Write the current compiler settings to
a file named 'path' in *.ini format.
- getConfigString() Return a string containing the current
compiler settings in *.ini format.
Do not override these in a subclass or assign to them as attributes
if your template will be used as a servlet, otherwise Webware will behave
unpredictably. However, it is OK to put same-name variables in the
searchList, because Webware does not use the searchList.
EXCEPTION: It's OK to override awake and sleep as long as you
call the superclass methods. (See section
13.4.)
- * isControlledByWebKit True if this template instance is
part of a live transaction in a running WebKit servlet.
- * isWebwareInstalled True if Webware is installed and the
template instance inherits from WebKit.Servlet. If not, it inherits
from Cheetah.Servlet.DummyServlet.
- * awake(transaction) Called by WebKit at the beginning of
the web transaction.
- * sleep(transaction) Called by WebKit at the end of the
web transaction.
- * respond(transaction) Called by WebKit to produce the
web transaction content. For a template-servlet, this means
filling the template.
- shutdown() Break reference cycles before deleting instance.
- * serverSidePath() The filesystem pathname of the
template-servlet (as opposed to the URL path).
- transaction The current Webware transaction.
- application The current Webware application.
- response The current Webware response.
- request The current Webware request.
- session The current Webware session.
- write Call this method to insert text in the filled template
output.
Several other goodies are available to template-servlets under the
request
attribute, see section 14.7.
transaction
, response
, request
and session
are
created from the current transaction when WebKit calls awake
, and don't
exist otherwise. Calling awake
yourself (rather than letting WebKit
call it) will raise an exception because the transaction
argument won't
have the right attributes.
These are accessible only if Cheetah knows Webware is installed. This
listing is based on a CVS snapshot of Webware dated 22 September 2002, and
may not include more recent changes.
The same caveats about overriding these methods apply.
- name() The simple name of the class. Used by Webware's logging and
debugging routines.
- log() Used by Webware's logging and debugging routines.
- canBeThreaded() True if the servlet can be multithreaded.
- canBeReused() True if the servlet can be used for another transaction
after the current transaction is finished.
- serverSideDir() Depreciated by
.serverSidePath()
.