如何将多个模板传递给flask.render_template() [英] How to pass multiple templates to flask.render_template()

查看:52
本文介绍了如何将多个模板传递给flask.render_template()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试部署flask应用程序,并且我希望将flask.render_template()方法与html文件列表一起传递.在这里,我看到它是合格的. http://flask.pocoo.org/docs/0.12/api/#flask.render_template

I'm trying to deploy a flask app and I want a flask.render_template() method passed with a list of html files. Here I see it's eligible. http://flask.pocoo.org/docs/0.12/api/#flask.render_template

我正在尝试使用此代码

from flask import Flask, render_template
app = Flask(__name__)
app.debug = True

@app.route('/')
def hello():
    templs = ["_header.html", "_footer.html"]
    return render_template(templs)

if __name__== '__main__':
    app.run()

但是实际上服务器仅返回列表中的第一个模板.如何遍历此列表以呈现列表中的所有模板?

But actually server returns only the first template from the list. How to iterate though this list in order to render all templates from the list?

谢谢,亚历克斯

推荐答案

据我所知,您正在尝试呈现静态页眉和页脚.我建议准备类似"layout.html"的内容,并附带页眉和页脚:

As far as i see you are trying to render static header and footer. I'd recommend to prepare something like "layout.html" with included header and footer:

//layout.html
<html>
    <head>//headhere</head>
    <header>//your static header</header>
    <main>
        {% block body %}
        //content will be here
        {% endblock %}
    </main>
    <footer> //your static footer </footer>
</html>

然后在子"模板(例如:index.html)中使用:

then in "child" templates(ex: index.html)use:

//index.html
{% extends "layout.html" %}
{% block body %}
   //your code here
{% endblock %}

它将从layout.html渲染页眉和页脚,并从index.html渲染其余页.

It will render header and footer from layout.html and rest from index.html.

这篇关于如何将多个模板传递给flask.render_template()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆