Performance: Block definitions and overriding
This affects page load speed and rendering by controlling how templates are composed and reused, impacting HTML size and browser rendering.
{% block content %}
<div>
<p>Base content</p>
{% block extra_content %}{% endblock %}
</div>
{% endblock %}
<!-- Child templates override only 'extra_content' block to add custom parts -->{% block content %}
<div>
<p>Static content repeated in many templates</p>
</div>
{% endblock %}
<!-- Overriding blocks by copying entire block content in child templates -->| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Copy entire block content in child templates | More DOM nodes due to duplicated HTML | Multiple reflows from larger DOM | Higher paint cost from complex layout | [X] Bad |
| Override small nested blocks only | Fewer DOM nodes, minimal duplication | Single reflow from smaller DOM | Lower paint cost from simpler layout | [OK] Good |
{% block %} in Flask templates?{% block %} tag defines a section in a base template that child templates can replace or extend.content in a child template?{% block block_name %}...{% endblock %} in the child template.{% block %} and {% endblock %} tags with the block name.base.html with:{% raw %}{% block title %}Base Title{% endblock %}{% endraw %}and child template child.html with:{% raw %}{% extends 'base.html' %}{% block title %}Child Title{% endblock %}{% endraw %}title block when rendering child.html?title block.{% raw %}{% extends 'base.html' %}{% block content %}Hello{% endblock content %}{% endraw %}{% endblock %} without repeating the block name.{% endblock content %}, which is invalid and causes a syntax error.{% raw %}{% block header %}Base Header{% endblock %}{% block footer %}Base Footer{% endblock %}{% endraw %}footer block but keep the base header. Which is the correct child template code?footer, so header remains from base.Pass the test and this pattern moves unknown