1

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.

drhanlau
  • 2,517
  • 2
  • 24
  • 42

4 Answers4

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

That should do the trick.

filmor
  • 30,840
  • 6
  • 50
  • 48
  • Yup, this one works. I tried before but I didn't declare it in the global scope or using the self.multiple. – drhanlau May 10 '11 at 13:10
2

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.

Cédric Julien
  • 78,516
  • 15
  • 127
  • 132
1

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)])
patrick
  • 11
  • 1
0

Why not just import Main in the template?

#from Main import multiple
<body>
    <div>
       $multiple(2,3)
    </div>
</body>
phd
  • 82,685
  • 13
  • 120
  • 165