drhanlau
drhanlau

Reputation: 2527

Cheetah template engine calling python base function

I am using Cheetah template together with Cherrypy, below is my main python file

Main.py:
def multiple(a,b):
    return a*b

def index(self):
    t = Template('template.tmpl')
    #blah implementation here

In my template file, I wish to achieve

<body>
    <div>
       $multiple(2,3)
    </div>
</body>

Anyone has an idea how can I get this implement? Many thanks.

Regards, Andy.

Upvotes: 1

Views: 1241

Answers (4)

phd
phd

Reputation: 94602

Why not just import Main in the template?

#from Main import multiple
<body>
    <div>
       $multiple(2,3)
    </div>
</body>

Upvotes: 0

patrick
patrick

Reputation: 11

This may answer it:

import Cheetah
import Cheetah.Template


def multiple(a,b):
    return a*b

print Cheetah.Template.Template(file='template.tmpl',
                                searchList=[dict(multiple=multiple)])

Upvotes: 1

filmor
filmor

Reputation: 32240

t = Template("template.tmpl")
t.multiple = multiple

That should do the trick.

Upvotes: 3

C&#233;dric Julien
C&#233;dric Julien

Reputation: 80791

try with the searchList argument :

def index(self):
    t = Template('template.tmpl', searchList=[multiple])

It allows you to define "placeholders" that you will be able to use in template definition.

Upvotes: 2

Related Questions