Syntax:
#implements METHOD
You can call any #def
or #block
method directly and get its
outpt. The top-level content - all the text/placeholders/directives outside any
#def
/#block
- gets concatenated and wrapped in a ``main
method'', by default .respond()
. So if you call .respond()
, you
get the ``whole template output''. When Webware calls .respond()
,
that's what it's doing. And when you do 'print t' or 'str(t)' on a template
instance, you're taking advantage of the fact that Cheetah makes
.__str__()
an alias for the main method.
That's all fine and dandy, but what if your application prefers to call another
method name rather than .respond()
? What if it wants to call, say,
.send_output()
instead? That's where #implements
steps in. It
lets you choose the name for the main method. Just put this in your template
definition:
#implements send_output
When one template extends another, every template in the inheritance chain has its own main method. To fill the template, you invoke exactly one of these methods and the others are ignored. The method you call may be in any of the templates in the inheritance chain: the base template, the leaf template, or any in between, depending on how you structure your application. So you have two problems: (1) calling the right method name, and (2) preventing an undesired same-name subclass method from overriding the one you want to call.
Cheetah assumes the method you will call is .respond()
because
that's what Webware calls. It further assumes the desired main method is the
one in the lowest-level base template, because that works well with
#block
as described in the Inheritance Approach for building Webware
servlets (section 14.2), which was originally the
principal use for Cheetah. So when you use #extends
, Cheetah changes
that template's main method to .writeBody()
to get it out of the way and
prevent it from overriding the base template's .respond()
.
Unfortunately this assumption breaks down if the template is used in other
ways. For instance, you may want to use the main method in the highest-level
leaf template, and treat the base template(s) as merely a library of
methods/attributes. In that case, the leaf template needs #implements
respond
to change its main method name back to .respond()
(or whatever
your application desires to call). Likewise, if your main method is in one of the
intermediate templates in an inheritance chain, that template needs
#implements respond
.
The other way the assumption breaks down is if the main method is in
the base template but that template extends a pure Python class. Cheetah sees
the #extends
and dutifully but incorrectly renames the method to
.writeBody()
, so you have to use #implements respond
to change
it back. Otherwise the dummy .respond()
in Cheetah.Template
is found, which outputs... nothing. So if you're using #extends
and get no output, the first thing you should think is, ``Do I need to
add #implements respond
somewhere?''