猎豹模板引擎调用python基本功能

时间:2011-05-10 12:48:23

标签: python cherrypy cheetah

我和Cherrypy一起使用Cheetah模板,下面是我的主要python文件

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

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

在我的模板文件中,我希望实现

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

任何人都知道如何才能获得此工具?非常感谢。

此致 安迪。

4 个答案:

答案 0 :(得分:2)

尝试使用searchList参数:

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

它允许您定义可以在模板定义中使用的“占位符”。

答案 1 :(得分:2)

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

这应该可以解决问题。

答案 2 :(得分:1)

这可能会回答:

import Cheetah
import Cheetah.Template


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

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

答案 3 :(得分:0)

为什么不在模板中导入Main?

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