Search code examples
pythoncheetah

Un/bound methods in Cheetah


Is there a way to declare static methods in cheetah? IE

snippets.tmpl

#def address($address, $title)
<div class="address">
<b>$title</h1></b>
#if $address.title
$address.title <br/>
#end if
$address.line1 <br/>
#if $address.line2
$address.line2 <br/>
#end if
$address.town, $address.state $address.zipcode
</div>
#end def

....

other snippets

other.tmpl

#from snippets import *

$snippets.address($home_address, "home address")

This code reports this error: NotFound: cannot find 'address'. Cheetah is compiling it as a bound method, natch:

snippets.py

class snippets(Template):

    ...

    def address(self, address, title, **KWS):

Is there a way to declare static methods? If not, what are some alternative ways to implement something like this (a snippets library)?


Solution

  • This page seems to have some relevant information, but I'm not in a position to try it out myself right now, sorry.

    Specifically, you should just be able to do:

    #@staticmethod
    #def address($address, $title)
    

    ...and have it work.

    (If you didn't know, staticmethod is a built-in function that creates a... static method :) It's most commonly used as a decorator. So I found that page by Googling "cheetah staticmethod".)