【问题标题】:cheetah template importing functionscheetah 模板导入函数
【发布时间】:2011-06-06 16:51:25
【问题描述】:

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

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

在 cmets 内部我有一些看起来像这样的东西

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

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

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

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

我错过了什么?

【问题讨论】:

    标签: python templates cheetah


    【解决方案1】:

    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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多