猎豹模板导入功能

时间:2011-06-06 16:51:26

标签: python templates 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。'

我错过了什么?

1 个答案:

答案 0 :(得分:0)

Cheetah将模板编译为Python类。导入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> 是对其方法的调用。为了简化代码,可以导入一下类:

span#a
相关问题