How to include an HTML file in a Jinja2 template?

Spread the love

Sometimes, we want to include an HTML file in a Jinja2 template.

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

How to include an HTML file in a Jinja2 template?

To include an HTML file in a Jinja2 template, we 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 %}

to include content1.html and content2.html with include.

Conclusion

To include an HTML file in a Jinja2 template, we use the include directive.

Leave a Reply

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