因此,我在尝试导入函数并在猎豹模板中运行它们时遇到了一些问题。
我有一个文件位于/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。
我遗漏了什么?
发布于 2017-05-09 18:00:46
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>https://stackoverflow.com/questions/6255358
复制相似问题