Flask模板高级技巧

个人名片
在这里插入图片描述
🎓作者简介:java领域优质创作者
🌐个人主页码农阿豪
📞工作室:新空间代码工作室(提供各种软件服务)
💌个人邮箱:[2435024119@qq.com]
📱个人微信:15279484656
🌐个人导航网站www.forff.top
💡座右铭:总有人要赢。为什么不能是我呢?

  • 专栏导航:

码农阿豪系列专栏导航
面试专栏:收集了java相关高频面试题,面试实战总结🍻🎉🖥️
Spring5系列专栏:整理了Spring5重要知识点与实战演练,有案例可直接使用🚀🔧💻
Redis专栏:Redis从零到一学习分享,经验总结,案例实战💐📝💡
全栈系列专栏:海纳百川有容乃大,可能你想要的东西里面都有🤸🌱🚀

Flask模板高级技巧

四、控制语句

4.1 条件判断

{% if user.age < 18 %}
    <p>未成年用户</p>
{% elif user.age > 60 %}
    <p>老年用户</p>
{% else %}
    <p>成年用户</p>
{% endif %}

4.2 循环语句

<table>
    <thead>
        <tr>
            <th>序号</th>
            <th>标题</th>
            <th>内容</th>
        </tr>
    </thead>
    <tbody>
        {% for post in posts %}
        <tr class="{{ loop.cycle('odd', 'even') }}">
            <td>{{ loop.index }}</td>
            <td>{{ post.title }}</td>
            <td>{{ post.content }}</td>
        </tr>
        {% else %}
        <tr>
            <td colspan="3">暂无文章</td>
        </tr>
        {% endfor %}
    </tbody>
</table>

循环变量说明:

  • loop.index: 当前迭代次数(从1开始)
  • loop.index0: 当前迭代次数(从0开始)
  • loop.revindex: 反向迭代次数
  • loop.first: 是否第一次迭代
  • loop.last: 是否最后一次迭代
  • loop.length: 序列长度

4.3 宏定义(模板函数)

定义宏:

{% macro render_comment(comment) %}
<div class="comment">
    <p>{{ comment.author }} 说:</p>
    <blockquote>{{ comment.content }}</blockquote>
</div>
{% endmacro %}

使用宏:

{{ render_comment(comment) }}

<!-- 导入其他模板中的宏 -->
{% from 'macros.html' import render_comment %}

五、模板继承

5.1 基础模板(base.html)

<!DOCTYPE html>
<html>
<head>
    <title>{% block title %}默认标题{% endblock %}</title>
    {% block head %}
    <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}">
    {% endblock %}
</head>
<body>
    <div class="container">
        {% block content %}
        <h1>默认内容</h1>
        {% endblock %}
    </div>
    
    {% block footer %}
    <footer>
        <p>&copy; 2023 My App</p>
    </footer>
    {% endblock %}
</body>
</html>

5.2 子模板继承

{% extends "base.html" %}

{% block title %}用户主页 - {{ super() }}{% endblock %}

{% block head %}
    {{ super() }}
    <style>
        .profile { color: blue; }
    </style>
{% endblock %}

{% block content %}
    <div class="profile">
        <h1>{{ user.username }}的个人资料</h1>
        <p>年龄: {{ user.age }}</p>
    </div>
{% endblock %}

{% block footer %}
    <footer>
        <p>&copy; 2023 用户中心</p>
    </footer>
{% endblock %}

5.3 包含其他模板

<!-- 包含头部 -->
{% include 'header.html' %}

<!-- 带参数包含 -->
{% include 'user_card.html' with user=current_user %}

<!-- 忽略缺失模板 -->
{% include 'sidebar.html' ignore missing %}

六、加载静态文件

6.1 静态文件组织

标准项目结构:

myapp/
├── app.py
├── static/
│   ├── css/
│   ├── js/
│   └── images/
└── templates/

6.2 引用静态文件

<!-- CSS文件 -->
<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">

<!-- JavaScript文件 -->
<script src="{{ url_for('static', filename='js/main.js') }}"></script>

<!-- 图片 -->
<img src="{{ url_for('static', filename='images/logo.png') }}" alt="Logo">

<!-- 使用缓存清除 -->
<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css', v=1.0) }}">

6.3 静态文件版本控制

在配置中添加版本号:

app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 3600  # 1小时缓存
app.config['STATIC_VERSION'] = '1.0.0'

模板中使用:

<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}?v={{ config.STATIC_VERSION }}">

6.4 使用CDN资源

{% if config.CDN_ENABLED %}
    <script src="https://cdn.example.com/jquery/3.6.0.min.js"></script>
{% else %}
    <script src="{{ url_for('static', filename='js/jquery.min.js') }}"></script>
{% endif %}

总结

在这两篇教程中,我们全面学习了Flask模板系统的各个方面:

  1. 模板渲染基础与原理
  2. 各种对象属性的访问方式
  3. 内置过滤器和自定义过滤器
  4. 条件判断和循环控制语句
  5. 宏定义和模板继承体系
  6. 静态文件管理和版本控制

这些知识构成了Flask前端开发的基础,掌握它们后,你已经能够构建结构清晰、易于维护的Web应用界面。在接下来的专栏中,我们将深入探讨Flask的表单处理、数据库集成和用户认证等高级主题。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码农阿豪@新空间

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值