templates/index.html
Hello World!
templates/user.html
Hello, {{ name }}!
渲染模板:
from flask import Flask,render_template
@app.route('/')
def index():
return render_template('index.html')
@app.route('/user/')
def user(name):
return render_template('user.html', name=name)
A value from a dictionary: {{ mydict['key'] }}.
A value from a list: {{ mylist[3] }}.
A value from a list, with a variable index: {{ mylist[myintvar] }}.
A value from an object's method: {{ myobj.somemethod() }}.
Hello, {{ name|capitalize }}
name|capitalize
变量过滤:capitalize
safe 不转义
lower
upper
title
trim
striptags
控制语句
{% if user %}
Hello, {{ user }}!
{% else %}
Hello, Stranger!
{% endif %}
{% for comment in comments %}
{{ comment }}
{% endfor %}
使用宏
{% macro render_comment(comment) %}
{{ comment }}
{% endmacro %}
{% for comment in comments %}
{{ render_comment(comment) }}
{% endfor %}
包含
{% include 'common.html' %}
block占位符
{% block head %}
{% block title %}{% endblock %} - My Application
{% endblock %}
{% block body %}
{% endblock %}
extends
{% extends "base.html" %}
{% block title %}Index{% endblock %}
{% block head %}
{{ super() }}
{% endblock %}
{% block body %}
Hello, World!
{% endblock %}