Categories
Python Answers

How to include a HTML file in a Jinja2 template with Python Flask?

Spread the love

Sometimes, we want to include a HTML file in a Jinja2 template with Python Flask.

In this article, we’ll look at how to include a HTML file in a Jinja2 template with Python Flask.

How to include a HTML file in a Jinja2 template with Python Flask?

To include a HTML file in a Jinja2 template with Python Flask, w ecan use the include directive.

For instance, we write

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

in our Jinja template to include the content1.html and content2.html files with

{% include 'content1.html' %}

and

{% include 'content2.html' %}

Conclusion

To include a HTML file in a Jinja2 template with Python Flask, w ecan use the include directive.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *