Python cheetah模板导入功能

Python cheetah模板导入功能,python,templates,cheetah,Python,Templates,Cheetah,因此,我在尝试导入函数并在cheetah模板中运行它们时遇到了一些问题 所以我有一个文件位于/docroot/tmpl/base.html 然后是另一个文件/docroot/tmpl/comments.html 在评论中,我有一些类似的东西 #def generateComments($commentObj): code for generating comments #end def #import docroot.tmpl.comments as comments <div cla

因此,我在尝试导入函数并在cheetah模板中运行它们时遇到了一些问题

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

在评论中,我有一些类似的东西

#def generateComments($commentObj):
 code for generating comments
#end def
#import docroot.tmpl.comments as comments
<div class="commentlist">
 $comments.generateComments($commentObj)
</div>
然后在base.html中,我希望有这样的语法

#def generateComments($commentObj):
 code for generating comments
#end def
#import docroot.tmpl.comments as comments
<div class="commentlist">
 $comments.generateComments($commentObj)
</div>
#将docroot.tmpl.comments作为注释导入
$comments.generateComments($commentObj)
但是,当我运行该输出时,我只打印出comments.html的内容,包括原始txt中的#def generateComments


我缺少什么?

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

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

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

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