Flask的集中控制
想通过一个统一的机制,同时允许一些公共的逻辑
{% if args["NoUser"] %}
无用户!
{% else %}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>结果查询</title>
<style type="text/css">
/** 用户名称 **/
.usr{
display: inline-block;
width:100px;
}
.time{
display: inline-block;
width:200px;
}
.cred{
display: inline-block;
width:40px;
}
.amo{
display: inline-block;
width:40px;
}
</style>
</head>
<body>
{{ args["strContent"] | safe }}
</body>
</html>
{% endif %}
但是感觉不好,于是采用JinJa2的模板继承机制,在base.html中写入一个统一的公共处理:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"> <html lang="en"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> {% block pub %} <script src="/static/jquery.js" type="text/javascript" charset="gb2312"></script> <script type="text/javascript" charset="gb2312"> $(document).ready(function(){ alert("hi") }); </script> {% endblock %} </head> <body> <div id="content">{% block content %}{% endblock %}</div> <div id="footer"> {% block footer %} © Copyright 2008 by <a href="http://domain.invalid/">you</a>. {% endblock %} </div> </body>
在实际模板中调用即可:
{% extends "base.html" %}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>结果查询</title>
<% block pub %>
{{ super() }}
<% endblock %>
</head>
<body>
{{ args["strContent"] | safe }}
</body>
</html>
这样,所有页面就能比较统一简洁地处理了。
浙公网安备 33010602011771号