Flask-include and set statements

The include statement
can include a template into another template, similar to copying the code of a template to the specified location of another template

Assignment (set) statement
Sometimes we want to add variables to the template, this time the assignment statement (set) comes in handy

{% set name= ‘Ellen’ %}

Then in the future, you can use name instead of Ellen, and you can also assign him a list and tuple.

{% set navigation = [(‘index.html’,‘index’),(‘about.html’,‘About’)] %}

The variables created by the assignment statement are valid after it. If you do n’t want a variable to pollute the global environment, you can use the with statement to create an internal scope and put the set statement in it, so that the variables created are only in the with code block. Only effective

{% with %}
{% set foo = 42 %}
{{ foo }} foo is 42 here
{% endwith %}

You can also add variables directly after with, for example, the above writing can be modified as follows:

{% with foo =42 %}
{ { foo }}
{% endwith %}

These two methods are equivalent. Once the with code block is exceeded, the variable foo can no longer be used.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <ul>
        <li>新闻</li>
        <li>娱乐</li>
        <li>图片</li>
        <li>女人</li>
    </ul>

</body>
</html>

<footer>
    这是网站的底部
</footer>

{% import 'macro.html' as macro with context %}
{% from "macro.html" import input %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
{% include 'commit/header.html' %}
{{ macro.input(value="有值了") }}
{{ input('password',type='password') }}
<input type ='type' value='name'>
<!-- {{ 变量 }} -->
{# {% 函数 if for %} #}


{% if username == "ellen1" %}
    <p>{{ username }}</p>
{% else %}
    <p>当前的用户名不是ellen...</p>
{% endif %}

{% for book in books %}
    <p>{{loop.index}}</p>
    <p>{{ book }}</p>
{% endfor %}

{% for user in users %}
    <p>{{ user }}</p>
{% endfor %}

{% for key,value in users.items() %}
    <p>{{ key }}</p>
    <p>{{ value }}</p>
{% endfor %}
<hr>
{% for key in users.keys() %}
    <p>{{ key }}</p>
{% endfor %}
<hr>
<for></for>
{% for value in users.values() %}
    <p>{{ loop.first }}</p>
    <p>{{ value }}</p>
{% endfor %}

{% include 'commit/footer.html' %}
</body>
</html>

-----------------------

Insert picture description here

Create detail.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<!--相续于templates这个目录来写绝对路径-->
{% include 'commit/header.html' %}
    <h1>这是我的详情页面</h1>
{% include 'commit/footer.html' %}
</body>
</html>

Insert picture description here
Set statement-global variable
{% with%}
{% set name = 'This is local'%}
{{name}}
{% endwith%}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
{% with %}
    {% set name = '这是局部的' %}
    {{ name }}
{% endwith %}
<!--相续于templates这个目录来写绝对路径-->
<!--全局变量-->
{% set name = 'ellen' %}
{% include 'commit/header.html' %}
    <h1>这是我的详情页面</h1>
    <p>{{ name }}</p>
{% include 'commit/footer.html' %}

<!--这是局部变量 -->
<p>{{ name }}</p>    # 显示全局,只有在with里显示的是局部的

{% with foo=66 %}
    <p>{{ foo }}</p>
{% endwith %}

{% with foo={"name":"ellen"} %}  # 定义变量为字典
    <p>{{ foo.name }}</p>
{% endwith %}

{% with foo=[1,2,3] %}   # 定义变量为列表
    {% for f in foo %}
    <p>{{ f }}</p>
    {% endfor %}
{% endwith %}

</body>
</html>
Published 118 original articles · won praise 0 · Views 2661

Guess you like

Origin blog.csdn.net/weixin_45905671/article/details/105465710