2010-03-31 54 views
0

由于性能原因建议here,我正在研究如何使用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() 

它看起来像反式是无,所以它去DummyTransaction,我错过了什么吗? 有关如何解决它的任何建议?

回答

0

你的主要问题是,在里面的myMethod()代替

print tmpl 

runner.py你需要

print results 

此外,你的代码有一些格式问题:

  1. 不要” t用反斜线标记$ {title}
  2. 您需要if __name__ == '__main__':而不是if name == 'main':