Performance: Why template engines matter
Template engines affect how fast the server can generate HTML and how efficiently the browser renders the page.
{% raw %}{% extends 'base.html' %}
{% block content %}
<p>{{ user_name }}</p>
{% endblock %}{% endraw %}html = "<html><body>" + user_name + "</body></html>" return html
| Pattern | Server Processing | HTML Size | Browser Parsing | Verdict |
|---|---|---|---|---|
| Manual string concatenation | High CPU time | Larger HTML | Slower parsing | [X] Bad |
| Jinja2 template engine with caching | Low CPU time | Smaller HTML | Faster parsing | [OK] Good |
name in a Flask template using Jinja2?{% if user %}
Hello, {{ user }}!
{% else %}
Hello, Guest!
{% endif %}user is set to "Alice"?user has a value. If yes, it shows "Hello, {{ user }}!" with the variable replaced.user is "Alice", the output becomes "Hello, Alice!".{% for item in items %}
{{ item }
{% endfor %}Pass the test and this pattern moves unknown