我使用Flask微框架为我的服务器使用Jinja模板。我有父template.html和一些childs child1.html,child2.html。这些孩子中的一些是相当大的html文件,我想不知何故拆分他们为了更好的清晰度在我的工作。
main.py:
from flask import Flask, request, render_template
app = Flask(__name__)
@app.route('/')
@app.route('/')
def home(task=''):
return render_template('child1.html', task=task)
app.run()
简化的template.html:
{% block content %}
{% endblock %}
而魔术是在child1.html:
{% extends 'template.html' %}
{% block content %}
{% if task == 'content1' %}
{% endif %}
{% if task == 'content2' %}
{% endif %}
{% endblock %}
而不是评论
我有很多html文本。并且很难跟踪变化,而不是犯一些错误,这是很难找到和纠正。所以我想加载content1.html,而不是在child1.html中写所有。我遇到这个问题Include another HTML file in a HTML file,但我有实现它的问题。我认为Jinja2可能有一个更好的工具。
注意。
上面的代码可能不能正常工作,我只是写它来ilustrate的问题。