0

出于性能方面的建议我正在研究如何使用 pr 编译的模板。

hello.tmpl在模板目录中编辑为

#attr title = "This is my Template"
<html>
    <head>
        <title>\${title}</title>
    </head>
    <body>
        Hello \${who}!
    </body>
</html>

然后发出cheetah-compile.exe .\hello.tmpl并得到hello.py

在另一个 python 文件runner.py中,我有:

#!/usr/bin/env python

from Cheetah.Template import Template
from template import hello
def myMethod():
    tmpl = hello.hello(searchList=[{'who' : 'world'}])
    results = tmpl.respond()
    print tmpl


if __name__ == '__main__':
    myMethod()

但结果是

<html>
    <head>
        <title>${title}</title>
    </head>
    <body>
        Hello ${who}!
    </body>
</html>

调试了一会,发现里面hello.py

def respond(self, trans=None):



    ## CHEETAH: main method generated for this template
    if (not trans and not self._CHEETAH__isBuffering and not callable(self.transaction)):
        trans = self.transaction # is None unless self.awake() was called
    if not trans:
        trans = DummyTransaction()

看起来 trans 是 None,所以它转到了DummyTransaction,我在这里错过了什么?关于如何修复它的任何建议?

4

1 回答 1

0

你的主要问题是在 runner.py 里面myMethod()而不是

print tmpl

你需要

print results

此外,您的代码存在一些格式问题:

  1. 不要用反斜杠转义 ${title}
  2. 你需要if __name__ == '__main__':而不是if name == 'main':
于 2010-03-31T23:45:06.740 回答