ACA
ACA

Reputation: 47

Django Template - use same block twice with different variables

I have a block called car_table and two kind of cars: blue and red. I want a template with two tables, one for the red cars and one for the blue cars, but I want to use just the 'general' block car_table.

in base_template.html I define the headers and general stuff

<!DOCTYPE html>

    blablabla...

    {% block content %}{% endblock %}

</html>

here is cars_table.html

{% extends base_template.html %}
{% block content %}

    blablabla...

    {% block table %}
    ...
        {% for car in cars %}
        <tr>
            <td>{{ car.name }}</td>
        </tr>
        {% endfor %}

    {% endblock %}

{% endblock %}

Now, I want a page with two tables: blue cars and red cars, using just the code in cars_table

Upvotes: 0

Views: 735

Answers (1)

yedpodtrzitko
yedpodtrzitko

Reputation: 9359

reposting the comments: How about having one template (eg. both_tables.html) and the template with the actual table would be included (via {% include %}) twice, each for one colour of cars, instead of extending the base template:

both_tables.html:

{% with red_cars as cars %}
    {% include car_table.html %}
{% endwith %}

{% with blue_cars as cars %}
    {% include car_table.html %}
{% endwith %}

car_table.html:

{% for car in cars %}
    <tr>
        <td>{{ car.name }}</td>
    </tr>
{% endfor %}

Upvotes: 1

Related Questions