Menu Close

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

Codeamend

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.

Posted in Python

You can also read...