题目要求

写一个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 def render(template, **kwargs):
   5     return lookup.get_template(template).render(**kwargs)
   6 
   7 urls = ('/', hello)
   8 
   9 class hello:
  10     def GET(self):
  11         return render('hello.mako', name='template')
  12         
  13 app = web.application(urls, globals())
  14 
  15 if __name__ == '__main__':
  16     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 即可。