我不知道如何操作,但想知道是否可以更改 jinja2 include 语句中的文件。
如: {% include file.name %} 到 {% include file1.name %}
由于 file.name 在包含括号中,我无法使用 {{ file.name }} 来实现此目的。
我想也许我可以使用 jquery 之类的东西,
$(".btnt").click(function(){
$("section:fourth").replaceWith("{% include 'file1.name' %}");}
也许通过单击按钮启动,这必须在同一页面上。我倾向于在大多数项目中使用 Flask python。
支持,您可以将要包含的页面作为变量传递给render_template,请参阅示例:
路线:
@main_blueprint.route("/")
def home():
return render_template("home.html", page="mypage.html")
home.html
神社模板:
<h1>this is home.html</h1>
{% include page %}
和
mypage.html
模板:
<p> this is from mypage.html </p>
它将打印两个模板中的内容。如果你 page="file_that_not_exists.html",flask 会因为找不到文件而抛出异常。