Template Inheritance and Reusable Templates

In Django, one of the key features that makes development efficient and convenient is template inheritance. With template inheritance, you can create a base or parent template that contains common elements and structures, and then use this template to build child templates that inherit these elements.

DRY: Don't Repeat Yourself

The DRY principle is a fundamental concept in software engineering, emphasizing the importance of avoiding repetition in code. Django's template inheritance follows this principle by allowing developers to define reusable templates that can be extended and customized by child templates.

Creating a Base Template

To start using template inheritance, you need to create a base or parent template. This template will contain the common elements shared by multiple child templates. For example, you might have a header, footer, and navigation bar that are consistent across your website's pages.

To create a base template, define a new HTML file and include the structural elements that you want to reuse. For instance, you can have placeholders for the content that will be specific to each child template.

<!-- base.html -->
<html>
<head>
    <title>My Website</title>
</head>
<body>
    <header>
        <!-- Header content -->
    </header>
    
    <nav>
        <!-- Navigation content -->
    </nav>
    
    <main>
        <!-- Content specific to each child template -->
        {% block content %}
        {% endblock %}
    </main>
    
    <footer>
        <!-- Footer content -->
    </footer>
</body>
</html>

Within the base.html template, the {% block %} tags define areas where the child templates can insert their specific content.

Extending the Base Template

Once the base template is defined, you can create child templates that inherit its structure and content placeholders. To extend the base template, use the {% extends %} tag at the beginning of the child template and specify the path to the base template.

<!-- homepage.html -->
{% extends 'base.html' %}

{% block content %}
    <!-- Content specific to the homepage -->
{% endblock %}

In the example above, homepage.html is a child template that extends base.html. It replaces the content within the {% block content %} tags with its unique content.

Reusable Templates

Apart from template inheritance, Django also allows you to create reusable templates known as includes. These templates represent self-contained UI elements that can be used in multiple locations across different pages or even within other templates.

Let's say you have a search bar that appears in your header on various pages. Instead of defining the search bar markup in each template, you can extract it into a reusable template.

<!-- search_bar.html -->
<form action="/search" method="get">
    <input type="text" name="query"/>
    <button type="submit">Search</button>
</form>

To include this template, use the {% include %} tag in any template where you want to display the search bar.

<!-- base.html -->
<html>
<head>
    <title>My Website</title>
</head>
<body>
    <header>
        <!-- Header content -->
        {% include 'search_bar.html' %}
    </header>
    
    ...
</body>
</html>

By using the {% include %} tag, you can easily reuse the search bar template without duplicating its code across multiple templates.

Conclusion

Template inheritance and reusable templates offer powerful features that promote code reusability and maintainability in Django projects. By defining a base html template and using {% extends %} and {% include %} tags, you can streamline development, follow the DRY principle, and easily manage consistent UI elements across multiple pages.


noob to master © copyleft