0

ここで提案されているパフォーマンス上の理由から、pr-compiled テンプレートの使用方法を研究しています。

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()

トランスが 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 に答える