Archarachne
Archarachne

Reputation: 345

Django template inheritance - markup CSS class

I would like to make a master.html for inheritance, but my problem is that in 3 different places the code is the same except the body class.

So I have master.html

<html>
 <head>...<head>
 <body>
  {% block one %}{% endblock %}
  {% block two %}{% endblock %}
  {% block extra %}{% endblock %}
 </body>
</html>

But in some places I have <_body class="front"> <_body class="not_front"> The rest of my content (like .js files, images) is the same.

Is there any clean way do it right and not to have three different 'masters'?

Upvotes: 0

Views: 86

Answers (1)

xjtian
xjtian

Reputation: 1006

You can define a block inside the <body> tag:

<html>
    <head>...<head>
    <body {% block body_options %}{% endblock %}>
        {% block one %}{% endblock %}
        {% block two %}{% endblock %}
        {% block extra %}{% endblock %}
    </body>
</html>

Then, in your child templates,

{% extends 'master.html' %}
{% block body_options %}class="front"{% endblock %}

Upvotes: 1

Related Questions