Differences between revisions 1 and 2
Revision 1 as of 2009-09-30 06:32:45
Size: 1227
Comment:
Revision 2 as of 2009-09-30 06:34:09
Size: 1267
Comment:
Deletions are marked like this. Additions are marked like this.
Line 30: Line 30:

lookup = TemplateLookup('templates')

题目要求

写一个Hello,template程序,使用模板来显示Hello,{name},其中name是变量,要由外部传入

框架说明

web.py (http://webpy.org)

步骤

在命令行下操作

1. 创建Project2项目

mkdir project2

2. 创建Hello App

cd project2
vi hello.py

   1 import web
   2 from mako.template import TemplateLookup
   3 
   4 lookup = TemplateLookup('templates')
   5 
   6 def render(template, **kwargs):
   7     return lookup.get_template(template).render(**kwargs)
   8 
   9 urls = ('/', hello)
  10 
  11 class hello:
  12     def GET(self):
  13         return render('hello.mako', name='template')
  14         
  15 app = web.application(urls, globals())
  16 
  17 if __name__ == '__main__':
  18     app.run()

4. 创建模板文件

cd project2
mkdir templates
vi hello.mako

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
  <title>Hello World</title>
  <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
</head>
<body>
<h1>Hello, ${name}</h1>
</body>
</html>

5. 结束

测试

cd project2
python hello.py

访问 http://localhost:8080 即可。

WebPyHelloTemplate (last edited 2009-12-25 07:15:49 by localhost)