2011-05-10 59 views
0

我使用獵豹模板,連同CherryPy的,下面是我的主要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> 

任何人有一個想法我怎麼能得到這個工具?非常感謝。

問候, 安迪。

回答

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

這應該做的伎倆。

+0

是啊,這一個工程。我以前嘗試過,但我沒有在全局範圍內聲明或使用self.multiple。 – cherhan 2011-05-10 13:10:02

2

嘗試用searchList說法:

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

它允許你定義「佔位符」,你就可以在模板定義中使用。

+0

您好我試過,但沒有奏效。 – cherhan 2011-05-10 13:10:40

+0

該死的,它只能使用變量,而不是方法:( – 2011-05-10 13:22:07

+0

是啊,對不起隊友:) – cherhan 2011-05-10 13:36:10

1

這可以回答這個問題:

import Cheetah 
import Cheetah.Template 


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

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

爲什麼不直接導入主模板?

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