网站首页 > 知识剖析 正文
测试器
测试器即对文件内变量名、变量类型等文件进行判断
- 语法
{% if 判断条件 %}
...代码块...
{% elif 判断条件 %}
...代码块...
{% else %}
...代码块...
{% endif %}
- 实例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{% set name='孟' %}
{% if names %}
<p>有name:{{ names }}</p>
{% elif name %}
<p>有name:{{ name }}</p>
{% else %}
<p>没有name;else</p>
{% endif %}
</body>
</html>
执行代码,可以看到页面打印:
也就是说代码走了elif。
上面代码也可这样写:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
{% set name='孟' %}
{% if names is undefined %}
<p>有name:{{ names }}</p>
{% elif name is defined %}
<p>有name:{{ name }}</p>
{% else %}
<p>没有name;else</p>
{% endif %}
</body>
</html>
模板继承
与方法继承相似
语法
{% extends '继承文件路径及后缀' %}
- 继承的好处
- 可以复用父类代码,节省开发时间
- 可以根据需求进行重写,比较灵活
- 如果想让子模板实现一些自定义内容,只需要使用block标签
实例
新建如下页面:
命名为base.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>模板继承</title>
<style>
* {
list-style: none;
text-decoration: none;
}
.header {
height: 60px;
background: #3a3a3a;
color: #fff;
margin-bottom: 20px;
}
.header .nav-group {
margin-left: 10px;
}
.header .nav-group li {
float: left;
line-height: 60px;
margin: 0px 20px;
}
.nav-group li a {
color: #fff;
}
.footer {
height: 100px;
background: #3a3a3a;
}
.footer p {
color: #fff;
margin-left: 30px;
padding-top: 20px;
}
.container{
overflow: hidden;
}
.left-container{
width: 800px;
height: 300px;
background: cadetblue;
float: left;
}
.right-container{
width: 600px;
height: 500px;
background: antiquewhite;
float: right;
}
</style>
</head>
<body>
<div class="header">
<ul class="nav-group">
<li><a href="#">新闻</a></li>
<li><a href="#">音乐</a></li>
<li><a href="#">贴吧</a></li>
<li><a href="#">视频</a></li>
</ul>
</div>
<div class="container">
<div class="left-container">
左面的盒子
</div>
<div class="right-container">
右面的盒子
</div>
</div>
<div class="footer">
<p>页面底部</p>
</div>
</body>
</html>
现在我们想增加两个页面,分别为detail.html和front_page.html,如果把代码原封不动的复制过去也可以,但是一旦页面有改动,那这三个文件你就得分别取改代码,所以这里就用继承就会方便很多了:
detail.html
{% extends 'base.html' %}
front_page.html同上,这样就通过继承复制出了两个相同的页面了。在app.py文件分别新增方法即可:
@app.route('/detail/')def detail():
return flask.render_template('detail.html')@app.route('/frontpage/')def frontPage():
return flask.render_template('front_page.html')
现在如果在detail.html页面要增加新内容怎么办呢?就是之前提过的,block标签上场了。
- 语法{% block 变量名 %}{% endblock %}
- 实例
将之前部分代码改为:
<div class="container">
<div class="left-container">
{% block left %}左面的盒子{% endblock %}
</div>
<div class="right-container">
{% block right %}右面的盒子{% endblock %}
</div>
</div>
然后在detail.html中修改代码为:
{% extends 'base.html' %}
{% block left %} <div>
<table border="2px" bordercolor='red'>
<thead>
<tr>
<td>编号</td>
<td>课程</td>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Flask</td>
</tr>
</tbody>
</table>
</div>{% endblock %}
在detail.html中的block添加代码后,就相当于把里面的代码添加到了base.html文件的block里面。
保存,detail.html页面变为:
以上内容即为模板继承。但是大家也发现左面盒子里面的内容全部被重写了,这时候只需要继承父类即可:
{{ super() }}
在detail.html中增加上面代码即可:
{% extends 'base.html' %}
{% block left %}
{{ super() }} <div>
<table border="2px" bordercolor='red'>
<thead>
<tr>
<td>编号</td>
<td>课程</td>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Flask</td>
</tr>
</tbody>
</table>
</div>{% endblock %}
现在如果说右侧也要有一个相同的table该怎么办呢?当然不需要再重复代码,只需要在对应的block中调用上一个block的名字:{{ self.block名字 }}即可。
- 实例
detail.html
{# 模板继承 #}{% extends 'base.html' %}{# 利用block修改模板内容 #}
{% block left %}
{{ super() }} <div>
<table border="2px" bordercolor='red'>
<thead>
<tr>
<td>编号</td>
<td>课程</td>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Flask</td>
</tr>
</tbody>
</table>
</div>{% endblock %}
{% block right %}
{{ super() }}
{{ self.left() }}
{% endblock %}
执行后右侧盒子就会和左侧一样了。
如果对接口、性能、自动化测试、面试经验交流等感兴趣的,可以关注我的头条号,我会不定期的发放免费的资料,这些资料都是从各个技术网站搜集、整理出来的,如果你有好的学习资料可以私聊发我,我会注明出处之后分享给大家。欢迎分享,欢迎评论,欢迎转发。需要资料的同学可以关注小编+转发文章+私信【测试资料】
- 上一篇: Vue的框架(了解)
- 下一篇: 巧用SqlServer数据库实现邮件自动发送功能
猜你喜欢
- 2024-11-25 服务器弱口令漏洞上传木马攻击实验
- 2024-11-25 10、Django 新建ipa(通讯录)项目
- 2024-11-25 Vue组件传参:Vue父组件向子组件传参
- 2024-11-25 第5天 | 16天搞定前端,html布局,表格和大块头
- 2024-11-25 如何应用“XML+XSLT”技术分离Web表示层数据和样式
- 2024-11-25 web前端ajax笔记之一
- 2024-11-25 vue3 新特性 computed、watch、watchEffect 看完就会
- 2024-11-25 巧用SqlServer数据库实现邮件自动发送功能
- 2024-11-25 Vue的框架(了解)
- 2024-11-25 web开发之-前端html(3)
- 最近发表
- 标签列表
-
- xml (46)
- css animation (57)
- array_slice (60)
- htmlspecialchars (54)
- position: absolute (54)
- datediff函数 (47)
- array_pop (49)
- jsmap (52)
- toggleclass (43)
- console.time (63)
- .sql (41)
- ahref (40)
- js json.parse (59)
- html复选框 (60)
- css 透明 (44)
- css 颜色 (47)
- php replace (41)
- css nth-child (48)
- min-height (40)
- xml schema (44)
- css 最后一个元素 (46)
- location.origin (44)
- table border (49)
- html tr (40)
- video controls (49)