1 模板引擎的作用
说它是引擎的原因是web应用程序可以调用模板中的函数,生成HTML代码,从而为生成用户界面。因此,它是整个界面生成的中枢,控制着生成界面的形式。
2 代码实现
先给出yate.py总体代码:
from string import Template
def start_response(resp="text/html"):
return('Content-type: ' + resp + '\n\n')
def include_header(the_title):
with open('templates/header.html') as headf:
head_text = headf.read()
header = Template(head_text)
return(header.substitute(title=the_title))
def include_footer(the_links):
with open('templates/footer.html') as footf:
foot_text = footf.read()
link_string = ''
for key in the_links:
link_string += '<a href="' + the_links[key] + '">' + key + '</a> '
footer = Template(foot_text)
return(footer.substitute(links=link_string))
def start_form(the_url, form_type="POST"):
return('<form action="' + the_url + '" method="' + form_type + '">')
def end_form(submit_msg="Submit"):
return('<p></p><input type=submit value="' + submit_msg + '"></form>')
def radio_button(rb_name, rb_value):
return('<input type="radio" name="' + rb_name +
'" value="' + rb_value + '"> ' + rb_value + '<br />')
def u_list(items):
u_string = '<ul>'
for item in items:
u_string += '<li>' + item + '</li>'
u_string += '</ul>'
return(u_string)
def header(header_text, header_level=2):
return('<h' + str(header_level) + '>' + header_text +
'</h' + str(header_level) + '>')
def para(para_text):
return('<p>' + para_text + '</p>')
2.1 开始响应start_response
def start_response(resp="text/html"):
return('Content-type: ' + resp + '\n\n')
将resp写入字符串中,默认resp="text/html"
。
2.2 页面头部
def include_header(the_title):
with open('templates/header.html') as headf:
head_text = headf.read()
header = Template(head_text)
return(header.substitute(title=the_title))
头部显示,用你想显示的字符the_title
替换templates/header.html
文件中的title
。实现方式是通关过调用Template类
的substitute方法
。这里有两点值得借鉴:
1.文件的读取整体读取方法
with open('templates/header.html') as >headf: head_text = headf.read()
2.文本中关键字的替换
Template类
的substitute方法
2.3 页面底部链接include_footer
def include_footer(the_links):
with open('templates/footer.html') as footf:
foot_text = footf.read()
link_string = ''
for key in the_links:
link_string += '<a href="' + the_links[key] + '">' + key + '</a> '
#将传入的"the_links"字典进行解读,字典中的’键‘,作为显示的文本;字典的’值‘作为链接的目标,从而形成一个超链接。
footer = Template(foot_text)
return(footer.substitute(links=link_string))
#最后,用字典中的超链接条目组成的字符串替换'templates/footer.html'中的'links'字符。
注意,给函数include_footer
传入的是字典,如以下调用的形式。
print(yate.include_footer({"Home":"/index.html"}))
2.4 初始表单的函数start_form
def start_form(the_url, form_type="POST"):
return('<form action="' + the_url + '" method="' + form_type + '">')
调用函数只需提供提交的地址url和形式type,太方便了,哈哈哈!!
2.5 提交表单的函数end_form
def end_form(submit_msg="Submit"):
return('<p></p><input type=submit value="' + submit_msg + '"></form>')
2.6 生成按钮函数radio_button
def radio_button(rb_name, rb_value):
return('<input type="radio" name="' + rb_name +
'" value="' + rb_value + '"> ' + rb_value + '<br />')
只需提供按钮的名称name和对应的值value.
2.7 生成列表函数u_list
def u_list(items):
u_string = '<ul>'
for item in items:
u_string += '<li>' + item + '</li>'
u_string += '</ul>'
return(u_string)
调用者只需提供列表类型的值给函数即可items。如调用者:
print(yate.u_list(athletes[athlete_name].top3))
将会列出运动员最好的三个成绩。
2.8 生成标题和段落格式函数
def header(header_text, header_level=2):
return('<h' + str(header_level) + '>' + header_text +
'</h' + str(header_level) + '>')
def para(para_text):
return('<p>' + para_text + '</p>')
比较简单,不再赘述。
3 效果检验
实现对以上函数的调用和执行,需要相关web服务器的支撑,在本章中用Python搭建了CGIHTTP服务器,自然能够调用执行这些函数,按照规范CGI程序必须放在与服务器同一目录下的cgi-bin文件夹,web应用文件夹组织如下所示:
按第七章的内容,我们查看cgi-bin文件夹下的generate_list.py文件,注意以下代码:
print yate.start_response()
print(yate.include_header("Coach Kelly's List of Athletes"))
print(yate.start_form("generate_timing_data.py"))
print(yate.para("Select an athlete from the list to work with:"))
for each_athlete in athletes:
print(yate.radio_button("which_athlete",athletes[each_athlete].name))
print(yate.end_form("Select"))
print(yate.include_footer({"Home":"/index.html"}))
这是一个生成显示页面的HTML语言的顺序。按照这种方式,我们可以得到想要的页面如下所示: