2

所以我在尝试导入函数并在我的 cheetah 模板中运行它们时遇到了一些麻烦。

所以我有一个位于 /docroot/tmpl/base.html 的文件,然后是另一个位于 /docroot/tmpl/comments.html 的文件

在评论里面我有一些看起来像这样的东西

#def generateComments($commentObj):
 code for generating comments
#end def

然后在base.html里面我想要这样的语法

#import docroot.tmpl.comments as comments
<div class="commentlist">
 $comments.generateComments($commentObj)
</div>

但是,当我运行该输出时,我只会打印出 comments.html 的内容,包括原始 txt 中的 #def generateComments。

我错过了什么?

4

1 回答 1

0

Cheetah 将模板编译为 Python 类。当您导入comments模块时,该模块由一个也名为comments. 您需要显式实例化该类并调用其generateComments方法。所以你的代码应该是

#from docroot.tmpl import comments
<div class="commentlist">
 $comments.comments().generateComments($commentObj)
</div>

第一个comments是模块,comments.comments是模块中的模板类,comments.comments()是类的实例,comments.comments().generateComments($commentObj)是对其方法的调用。为了简化代码,请导入类:

#from docroot.tmpl.comments import comments
<div class="commentlist">
 $comments().generateComments($commentObj)
</div>
于 2017-05-09T10:00:45.747 回答