由于here建议的性能原因,我正在研究如何使用预编译的模板。
我将模板目录中的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,我这里漏掉了什么?对如何修复有什么建议吗?
发布于 2010-04-01 07:45:07
您的主要问题是在myMethod()内部的runner.py中,而不是
print tmpl你需要
print results此外,您的代码还存在一些格式问题:
标题不要用反斜杠来转义${
标题您需要if __name__ == '__main__':而不是if name == 'main':
https://stackoverflow.com/questions/2550323
复制相似问题