Performance: Include for reusable fragments
This affects page load speed by reducing template parsing and rendering time through reuse of common HTML parts.
{% raw %}
<!-- base.html -->
<!DOCTYPE html>
<html>
<head><title>{% block title %}{% endblock %}</title></head>
<body>
{% include 'header.html' %}
{% block content %}{% endblock %}
{% include 'footer.html' %}
</body>
</html>
<!-- page.html -->
{% extends 'base.html' %}
{% block title %}Page{% endblock %}
{% block content %}
<p>Page content here</p>
{% endblock %}
{% endraw %}{% raw %}
<!DOCTYPE html>
<html>
<head>
<title>Page</title>
</head>
<body>
<header>
<h1>Site Title</h1>
<nav>...</nav>
</header>
<!-- page specific content -->
<footer>
<p>Ā© 2024 Company</p>
</footer>
</body>
</html>
{% endraw %}| Pattern | Template Parsing | Server CPU | HTML Size | Verdict |
|---|---|---|---|---|
| Duplicated fragments in every template | High (repeated parsing) | Higher CPU usage | Larger HTML templates | [X] Bad |
| Includes for reusable fragments | Low (parsed once) | Lower CPU usage | Smaller templates | [OK] Good |
{% include %} in Flask templates?{% include %}{% include %}.header.html inside another template?{% include 'filename' %} with quotes around the filename.{% import %} is for macros, {{ include() }} is invalid, and {% extend %} is for template inheritance.base.html is rendered?base.html:
<html>
<body>
{% include 'nav.html' %}
<h1>Welcome</h1>
</body>
</html>
nav.html:
<nav>Menu</nav>{% include 'nav.html' %} inserts the content of nav.html exactly where the tag is placed.base.html, the <nav>Menu</nav> from nav.html appears before the <h1>Welcome</h1> inside the body.{% include header.html %}'header.html'.header.html is unquoted, causing a syntax error.{% include %} to add footer.html to multiple pages and ensures easy updates?{% include 'footer.html' %} inserts the footer code wherever needed, keeping code DRY.{% extend %} is for inheritance, and writing HTML in route mixes logic and presentation.Pass the test and this pattern moves unknown