首页 技术 正文
技术 2022年11月23日
0 收藏 596 点赞 4,881 浏览 2007 个字

模版继承和block的目的就是为了减少前端代码量

flask_ones.py

#encoding:utf-8
from flask import Flask,url_for,redirect,render_templateapp = Flask(__name__)@app.route('/')
def index():
return render_template('index.html')@app.route('/login/')
def login():
return render_template('login.html')if __name__ == '__main__':
app.run(debug=True)

html文件

#################### index.html ##################<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.nav{
background:rebeccapurple;
height:65px;
}
ul{
overflow: hidden;
}
ul li{
float: left;
list-style: none; padding:0 10px;
line-height: 65px;
} ul li a{
color: white;
}
</style>
</head>
<body>
<div class="nav">
<ul>
<li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首页</a></li>
<li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >发布问答</a></li>
</ul>
</div>
<h1>这是index页面</h1>
</body>
</html>#####################login.html###################
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>登陆页面</title>
<style>
.nav{
background:rebeccapurple;
height:65px;
}
ul{
overflow: hidden;
}
ul li{
float: left;
list-style: none; padding:0 10px;
line-height: 65px;
} ul li a{
color: white;
}
</style>
</head>
<body>
<div class="nav">
<ul>
<li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首页</a></li>
<li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >发布问答</a></li>
</ul>
</div>
   <h1>这是login页面</h1>
</body>
</html>

对比index.html和login.html的代码发现,除了红色的代码部分,其余代码均一样,而如果有其他更多类似的页面,会加大代码的量,因此引出以下的模版继承和block

语法:

{% extends "base.html" %}{% block name %}
....
{% endblock %}

所以以上代码简化为:

同级目录新建一个common.html,将相同的代码部分通过继承带进去,如下:

common.html(公共代码部分)

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.nav{
background:rebeccapurple;
height:65px;
}
ul{
overflow: hidden;
}
ul li{
float: left;
list-style: none; padding:0 10px;
line-height: 65px;
} ul li a{
color: white;
}
</style>
</head>
<body>
<div class="nav">
<ul>
<li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首页</a></li>
<li><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >发布问答</a></li>
</ul>
</div>
{% block main %} {% endblock %}</body>
</html>

此时index.html和login.html代码为:

#index.html
{% extends "base.html" %}      #代表继承base.html{% block main %}                #这里的main要与base.html里blcok的设定的名字相同  
<h1>这是index页面</h1>           #将这里的内容传到base.html的block下,并在本文件里应用
{% endblock %}#login.html
{% extends "base.html" %}{% block main %}
<h1>这是登陆页面</h1>
{% endblock %}

flask模版继承和blockflask模版继承和block

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,000
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,512
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,358
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,141
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,771
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,849