猎豹模板导入功能
因此,我在尝试导入函数并在我的猎豹模板中运行它们时遇到了一些麻烦。
所以我有一个文件位于 /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 中的 #defgenerateComments。
我缺少什么?
So I am having some trouble trying to import functions and run them inside my cheetah templates.
So I have one file that lives at /docroot/tmpl/base.html
and then another file that is /docroot/tmpl/comments.html
inside of comments I have something that looks like this
#def generateComments($commentObj):
code for generating comments
#end def
then inside of base.html I want to have a syntax like this
#import docroot.tmpl.comments as comments
<div class="commentlist">
$comments.generateComments($commentObj)
</div>
However when I run that output I just get the contents of comments.html printed out including the #def generateComments in raw txt.'
What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Cheetah 将模板编译为 Python 类。当您导入
comments
模块时,该模块由一个也名为comments
的类组成。您需要显式实例化该类并调用其generateComments
方法。所以你的代码应该是第一个
comments
是一个模块,comments.comments
是模块中的模板类,comments.comments()
是类的实例,comments.comments().generateComments($commentObj)
是对其方法的调用。为了简化代码,导入该类:Cheetah compiles templates to Python classes. When you import
comments
module the module consists of a single class also namedcomments
. You need to explicitly instantiate the class and call itsgenerateComments
method. So your code should beThe first
comments
is a module,comments.comments
is a template class in the module,comments.comments()
is an instance of the class,comments.comments().generateComments($commentObj)
is a call to its method. To simplify the code a bit import the class: