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

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

问题描述

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

我有一个父级template.html和一些名为child1.htmlchild2.html的子级模板,其中一些子级模板是非常大的HTML文件,我希望以某种方式将它们拆分以更好地进行工作. >

我的main.py脚本的内容:

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()

简化的template.html:

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

魔力在child1.html中:

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

代替评论:

<!-- include content1.html -->

我有很多html文本,很难跟踪更改并且不犯一些错误,因此很难发现和纠正.

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

我遇到了这个问题,但是我遇到了实施时遇到问题.

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

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

解决方案

使用jinja2 指令.

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

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

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天全站免登陆