Performance: Template inheritance with base template
This affects the server-side rendering speed, impacting the initial page load time.
{% comment %}Base template with blocks{% endcomment %}
<!-- base.html -->
<html>
<head><title>{% block title %}Default Title{% endblock %}</title></head>
<body>
<header>Site header</header>
<main>{% block content %}{% endblock %}</main>
<footer>Site footer</footer>
</body>
</html>
{% comment %}Child template{% endcomment %}
{% extends "base.html" %}
{% block title %}Page 1{% endblock %}
{% block content %}Content for page 1{% endblock %}{% comment %}Each page repeats full HTML structure{% endcomment %}
<html>
<head><title>Page 1</title></head>
<body>
<header>Site header</header>
<main>Content for page 1</main>
<footer>Site footer</footer>
</body>
</html>| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Repeated full HTML in each template | No change (server-side) | No change (browser) | Larger templates increase server parsing time | [X] Bad |
| Template inheritance with base template | No change (server-side) | No change (browser) | Smaller templates reduce server parsing time | [OK] Good |
{% extends %} in a Django template?{% extends %}{% extends %} = inherit base template [OK]{% block blockname %} and closes with {% endblock %} without quotes.{% raw %}{% block title %}Base Title{% endblock %}{% block content %}Base Content{% endblock %}{% endraw %} and a child template {% raw %}{% extends 'base.html' %}{% block title %}Child Title{% endblock %}{% endraw %}, what will be the rendered output for the content block when rendering the child template?title block, so the content block remains as defined in the base template.content, the base template's Base Content is used.{% raw %}{% extends 'base.html' %}{% block content %}Hello World{% endblock content %}{% endraw %}{% endblock %} without repeating the block name.header, content, and footer. You want a child template to reuse the base layout but add a sidebar inside the content block without losing the original content. Which is the correct way to do this?{{ block.super }} inside a block includes the original content from the base template's block.Pass the test and this pattern moves unknown