Flask-include和set语句

include语句
include语句可以把一个模版引入到另外一个模板中,类似于把一个模板的代码copy到另外一个模板的指定位置

赋值(set)语句
有时候我们想在模板中添加变量,这时候赋值语句(set)就派上用场了

{% set name= ‘Ellen’ %}

那么以后就可以使用name来代替Ellen这个值了,同时,也可以给他赋值列表和元组。

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

赋值语句创建的变量在其之后都是有效的,如果不想让一个变量污染全局环境,可以使用with语句来创建一个内部的作用域,将set语句放在其中,这样创建的变量只在with代码块中才有效

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

也可以在with的后面直接添加变量,比如以上的写法可以修改成这样:

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

这两种方式都是等价的,一旦超出with代码块,就不能再使用foo这上变量了。

<!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>

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

在这里插入图片描述

创建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>

在这里插入图片描述
赋值(set)语句 - 全局变量
{% with %}
{% set name = ‘这是局部的’ %}
{{ 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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值