如何在 Jinja2 模板中包含 HTML 文件? [英] How do I include a HTML file in a Jinja2 template?

查看:55
本文介绍了如何在 Jinja2 模板中包含 HTML 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 Jinja 模板的服务器上使用 Flask 微框架.

我有一个父 template.html 和一些名为 child1.htmlchild2.html 的子模板,其中一些子模板是相当大的 HTML 文件,我想以某种方式拆分它们,以便更好地了解我的工作.

我的 main.py 脚本的内容:

from flask import Flask, request, render_templateapp = Flask(__name__)@app.route('/')@app.route('/<任务>')定义家(任务=''):return render_template('child1.html', task=task)应用程序运行()

简化的template.html:

<头></头><身体><div class="container">{% 块内容 %}{% 端块 %}

神奇在于child1.html:

{% 扩展 'template.html' %}{% 块内容 %}{% 如果任务 == 'content1' %}<!-- 包含 content1.html -->{% 万一 %}{% 如果任务 == 'content2' %}<!-- 包含 content2.html -->{% 万一 %}{% 结束块 %}

代替评论:

我有很多 html 文本,很难跟踪更改并且不犯一些错误,然后很难找到和更正.

我只想加载 content1.html 而不是全部写在 child1.html 中.

我遇到了这个问题,但我有实施过程中遇到的问题.

我认为 Jinja2 可能有更好的工具.

注意:上面的代码可能无法正常工作,我只是为了说明问题而写的.

解决方案

使用 jinja2 {% 包含 %} 指令.

{% 扩展 'template.html' %}{% 块内容 %}{% 如果任务 == 'content1' %}{% 包含 'content1.html' %}{% 万一 %}{% 如果任务 == 'content2' %}{% 包含 'content2.html' %}{% 万一 %}{% 结束块 %}

这将包括来自正确内容文件的内容.

I am using Flask micro-framework for my server which uses Jinja templates.

I have a parent template.html and some children templates called child1.html and child2.html, some of these children templates are pretty large HTML files and I would like to somehow split them for better lucidity over my work.

Contents of my main.py script:

from flask import Flask, request, render_template

app = Flask(__name__)

@app.route('/')
@app.route('/<task>')
def home(task=''):
  return render_template('child1.html', task=task)

app.run()

The simplified template.html:

<!DOCTYPE html>
<html>
  <head></head>
  <body>
    <div class="container">
      {% block content %}{% endblock %}
    </div>
  </body>
</html>

The magic is in child1.html:

{% extends 'template.html' %}
{% block content %}
  {% if task == 'content1' %}
    <!-- include content1.html -->
  {% endif %}
  {% if task == 'content2' %}
    <!-- include content2.html -->
  {% endif %}
{% endblock %}

Instead of the comments:

<!-- include content1.html -->

I have a lot of html text, and it is very hard to keep track of changes and not to make some mistakes, which are then pretty hard to find and correct.

I'd like to just load the content1.html instead of writing it all in child1.html.

I came across this question, but I had problems implementing it.

I think Jinja2 might have a better tool for that.

NOTE: The code above might not be working properly, I just wrote it to illustrate the problem.

解决方案

Use the jinja2 {% include %} directive.

{% extends 'template.html' %}
{% block content %}
    {% if task == 'content1' %}
        {% include 'content1.html' %}
    {% endif %}
    {% if task == 'content2' %}
        {% include 'content2.html' %}
    {% endif %}
{% endblock %}

This will include the content from the correct content-file.

这篇关于如何在 Jinja2 模板中包含 HTML 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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