views:

432

answers:

2

I have a master template file called base.html, in it I have the following code:

{% ifequal environment "dev" %}
    {% block stylesheets %}{% endblock %}
{% endifequal %}

I inherit this in other templates and do the following:

{% block stylesheets %}
    <link ... >
{% endblock %}

The problem is, the stylesheet I link never gets applied, the stylesheets block seems to be ignored whether the ifequal condition is met in the base or not.

+3  A: 

Edit (14th October, 2010):

The original question title is no longer true, according to this comment on a ticket on Django.

Original Answer:

I'm not sure why not, but you could just do:

{% block stylesheets %}
    {% ifequal environment "dev" %}
        ... something ....
    {% else %}
        {{ block.super }}
    {% endifequal %}
{% endblock %}

Having rethought this a bit - I guess that means repeating that logic inside each of your templates, which is fairly unsatisfactory, but I'll leave this answer here anyway. I've had a quick look through the Django tickets and can't find anything relevant.

Dominic Rodger
+2  A: 

According to the brief discussion on this ticket it seems that:

  1. It is intended by design that block definitions cannot be made conditionally.
  2. Yet someone claims it does work inside an "if", but not inside "ifequal".

If (2) is true, you might be able to make your case work if you are able to restructure your conditional as a simple "if."

Alternatively, you could look into the Django source and try to discover why it works with "if" and not "ifequal." The two tags are so similar that it seems likely you could work up a patch to make it work with both, in which case you could post it to that ticket and try to make a case on the django-developers mailing list why design decision (1) is wrong (though I'd advise waiting to do that until after 1.1 comes out). I'd also advise thinking carefully through the rats-nest of complicated edge cases that would be introduced by supporting conditional block-defining. Your use-case looks simple enough, but you need to consider more than just your use-case if you want to make a convincing argument to the Django devs.

Carl Meyer
+1 I can probably figure out workaround to make it an if block. I may try to build a patch too.
Soviut
+1ed for looking more thoroughly than me :)
Dominic Rodger