Jinja Template Inheritance
n
n
Template inheritance allows you to create a reusable base layout that child templates can extend and override specific content blocks within.
nn
n
Example:
n
Example: base.html and child.html
n
base.html (the skeleton)
n
<!DOCTYPE html>n<html lang="en">n<head>n <title>{% block title %}{% endblock %} - My Site</title>n</head>n<body>n <div id="header">n <h1>My Website</h1>n </div>n <div id="content">{% block content %}{% endblock %}</div>n <div id="footer">n <p>© 2024 My Site</p>n </div>n</body>n</html>
n
child.html (the specific page)
n
{% extends "base.html" %}nn{% block title %}Homepage{% endblock %}nn{% block content %}n <h2>Welcome to the Homepage</h2>n <p>This is the unique content for our homepage.</p>n{% endblock %}
n
When Jinja renders child.html, it will combine the content from the block tags in the child template with the skeleton from the base template. This results in a complete HTML document without repeating code.
nn
n
n
