• 在webpy中使用Cheetah模板引擎
    • 问题:
    • 解决:

    在webpy中使用Cheetah模板引擎

    问题:

    怎样在webpy中使用Cheetah模板引擎?

    解决:

    您需要先安装webpy(0.3)和Cheetah:http://www.cheetahtemplate.org/. 然后尝试使用下面的代码段:

    1. # encoding: utf-8
    2. # File: code.py
    3. import web
    4. from web.contrib.template import render_cheetah
    5. render = render_cheetah('templates/')
    6. urls = (
    7. '/(first)', 'first',
    8. '/(second)', 'second'
    9. )
    10. app = web.application(urls, globals(), web.reloader)
    11. class first:
    12. def GET(self, name):
    13. # cheetah template takes only keyword arguments,
    14. # you should call it as:
    15. # return render.hello(name=name)
    16. # Below is incorrect:
    17. # return render.hello(name)
    18. return render.first(name=name)
    19. class second:
    20. def GET(self, name):
    21. return render.first(**locals())
    22. if __name__ == "__main__":
    23. app.run()

    模板文件

    1. ## File: templates/first.html
    2. hello, $name.