一、模板渲染基础
1.1 为什么需要模板引擎
在web开发中,直接将html代码写在python文件中会导致诸多问题:
- 代码难以维护
- 前后端耦合严重
- 无法复用html组件
- 缺乏逻辑控制能力
flask内置了jinja2模板引擎,完美解决了这些问题。
1.2 第一个模板渲染示例
首先创建项目结构:
myapp/
├── app.py
└── templates/
└── index.html
app.py内容:
from flask import flask, render_template app = flask(__name__) @app.route('/') def index(): user = {'username': '张三', 'age': 25} posts = [ {'title': '第一篇', 'content': '内容1'}, {'title': '第二篇', 'content': '内容2'} ] return render_template('index.html', user=user, posts=posts) if __name__ == '__main__': app.run(debug=true)
templates/index.html内容:
<!doctype html> <html> <head> <title>{{ user.username }}的主页</title> </head> <body> <h1>欢迎, {{ user.username }}!</h1> <p>年龄: {{ user.age }}</p> <h2>文章列表</h2> <ul> {% for post in posts %} <li>{{ post.title }} - {{ post.content }}</li> {% endfor %} </ul> </body> </html>
1.3 模板渲染原理
render_template()函数的工作流程:
- 在templates目录查找指定模板文件
- 解析模板中的变量和逻辑
- 将上下文变量传入模板
- 生成最终html响应
二、模板访问对象属性
2.1 访问字典属性
<p>用户名: {{ user['username'] }}</p> <p>年龄: {{ user.get('age', 18) }}</p> <!-- 带默认值 -->
2.2 访问对象属性
假设我们有一个user类:
class user: def __init__(self, username, email): self.username = username self.email = email
模板中可以这样访问:
<p>用户名: {{ user.username }}</p> <p>邮箱: {{ user.email }}</p>
2.3 访问列表和元组
<p>第一篇文章: {{ posts[0].title }}</p> <p>最后一篇文章: {{ posts[-1].title }}</p>
2.4 特殊变量访问
<p>当前时间: {{ config.debug }}</p> <!-- 访问flask配置 --> <p>请求方法: {{ request.method }}</p> <!-- 访问请求对象 --> <p>会话信息: {{ session.get('user_id') }}</p> <p>闪现消息: {{ get_flashed_messages() }}</p>
三、过滤器的使用
3.1 内置过滤器大全
jinja2提供了丰富的内置过滤器:
<!-- 字符串处理 --> <p>{{ "hello"|capitalize }}</p> <!-- hello --> <p>{{ "hello"|lower }}</p> <!-- hello --> <p>{{ "hello world"|title }}</p> <!-- hello world --> <p>{{ "hello"|replace("e", "a") }}</p> <!-- hallo --> <!-- 列表处理 --> <p>{{ [1,2,3]|length }}</p> <!-- 3 --> <p>{{ [1,2,3]|first }}</p> <!-- 1 --> <p>{{ [1,2,3]|last }}</p> <!-- 3 --> <p>{{ [1,2,3]|join("|") }}</p> <!-- 1|2|3 --> <!-- 数值处理 --> <p>{{ 3.1415926|round(2) }}</p> <!-- 3.14 --> <p>{{ 1000|filesizeformat }}</p> <!-- 1000 bytes --> <p>{{ 0.85|float }}</p> <!-- 0.85 --> <!-- 日期处理 --> <p>{{ user.create_time|datetimeformat }}</p> <p>{{ user.create_time|datetimeformat('%y-%m-%d') }}</p> <!-- html处理 --> <p>{{ "<script>alert(1)</script>"|escape }}</p> <p>{{ "markdown text"|markdown }}</p> <p>{{ "https://example.com"|urlencode }}</p>
3.2 自定义过滤器
在app.py中注册自定义过滤器:
@app.template_filter('reverse') def reverse_filter(s): return s[::-1] @app.template_filter('format_phone') def format_phone(phone): return f"{phone[:3]}-{phone[3:7]}-{phone[7:]}"
模板中使用:
<p>{{ "hello"|reverse }}</p> <!-- olleh --> <p>{{ "13812345678"|format_phone }}</p> <!-- 138-1234-5678 -->
flask模板高级技巧
1.控制语句
条件判断
{% if user.age < 18 %} <p>未成年用户</p> {% elif user.age > 60 %} <p>老年用户</p> {% else %} <p>成年用户</p> {% endif %}
循环语句
<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: 序列长度
宏定义(模板函数)
定义宏:
{% 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 %}
2.模板继承
基础模板(base.html)
<!doctype html> <html> <head> <title>{% block title %}默认标题{% endblock %}</title> {% block head %} <link rel="stylesheet" href="{{ url_for('static', filename='style.css') }}" rel="external nofollow" > {% endblock %} </head> <body> <div class="container"> {% block content %} <h1>默认内容</h1> {% endblock %} </div> {% block footer %} <footer> <p>© 2023 my app</p> </footer> {% endblock %} </body> </html>
子模板继承
{% 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>© 2023 用户中心</p> </footer> {% endblock %}
包含其他模板
<!-- 包含头部 --> {% include 'header.html' %} <!-- 带参数包含 --> {% include 'user_card.html' with user=current_user %} <!-- 忽略缺失模板 --> {% include 'sidebar.html' ignore missing %}
3.加载静态文件
静态文件组织
标准项目结构:
myapp/
├── app.py
├── static/
│ ├── css/
│ ├── js/
│ └── images/
└── templates/
引用静态文件
<!-- css文件 --> <link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}" rel="external nofollow" > <!-- 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) }}" rel="external nofollow" >
静态文件版本控制
在配置中添加版本号:
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 }}" rel="external nofollow" >
使用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 %}
以上就是python中flask模板的使用与高级技巧详解的详细内容,更多关于python flask模板的资料请关注代码网其它相关文章!
发表评论