The most powerful part of Jinja is template inheritance. Template inheritance allows you to build a base “skeleton” template that contains all the common elements of your site and defines blocks that child templates can override.
例如:
<! base.html >
{% extends 'bootstrap/base.html' %}
{% block styles %}
{{ super() }}
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/styles.css') }}">
{% endblock %}
{% block content %}
<! your child templates will go here >
{% endblock %}
例如:
此基础模板首先从
flask-bootstrap
继承样式。然后定义指向styles.css
文件的链接。所有子模板的内容都将包含在block content
中。因此,无论您创建什么样的模板,只要它继承了这个基础模板,您在styles.css
中定义的样式将始终适用调用
home()
view函数时呈现的子模板(例如index.html
)如下所示:您可以使用关键字
extends
继承基础模板。如前所述,内容将出现在基本模板的block content
中,因此必须明确定义呈现此页面的路径可以如下所示:
因此,无论您创建什么样的模板,只要您继承了基础模板,您的样式都将应用于其中的每一个模板
阅读template inheritance doc的更多内容
确保该文件扩展了基础文件或任何包含css样式链接的文件
相关问题 更多 >
编程相关推荐