【案例演练】测试器与模板继承

测试器

测试器即对文件内变量名、变量类型等文件进行判断

  • 语法
{% 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{
             800px;
            height: 300px;
            background: cadetblue;
            float: left;
        }

        .right-container{
             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.htmlfront_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 %}

如果对软件测试、接口测试、自动化测试、面试经验交流感兴趣,可以加软件测试交流:1085991341,还会有同行一起技术交流。
执行后右侧盒子就会和左侧一样了。

以上内容希望对你有帮助,有被帮助到的朋友欢迎点赞,评论。

原文地址:https://www.cnblogs.com/Chaqian/p/12980547.html