Performance: Template inheritance with extends
This affects page load speed by reducing template rendering time and minimizing repeated HTML generation.
<!-- base.html -->
<html>
<head>
<title>{% block title %}My Site{% endblock %}</title>
</head>
<body>
<header>Header content</header>
{% block content %}{% endblock %}
</body>
</html>
<!-- child.html -->
{% extends 'base.html' %}
{% block content %}
Page-specific content
{% endblock %}<!-- base.html content repeated in every template -->
<html>
<head>
<title>My Site</title>
</head>
<body>
<header>Header content</header>
<!-- page-specific content here -->
</body>
</html>| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Repeated full templates | More nodes generated repeatedly | Multiple reflows due to larger DOM | Higher paint cost from larger layout | [X] Bad |
| Template inheritance with extends | Minimal nodes generated per page | Fewer reflows due to smaller DOM | Lower paint cost from focused layout | [OK] Good |
{% extends %} in Flask templates?{% extends %}{% extends %}.base.html?{% extends 'filename' %}.{% include %} inserts content but does not inherit; {% import %} is for macros; {% block %} defines sections, not inheritance.base.html with a block named content and a child template that extends it and overrides content, what will be displayed when rendering the child template?content block -> Option B{% extends 'base.html' %}
{% block content %}
Hello World
{% endblock %}, not with HTML-like closing tags.p tags are allowed inside blocks.{% endblock %} instead of -> Option Abase.html with two blocks: header and content. You want a child template to reuse the base layout but only change the content block, keeping the base header as is. Which is the correct approach?header block in the child; only override content.base.html and define only the content block; omit the header block -> Option DPass the test and this pattern moves unknown