Задать вопрос

Тел: +7 965 3737 888





Smart {% if %} template tag

<p>Save this as smart_if.py in the templatetags folder of one of your apps. Then a simple {% load smart_if %} replaces the boring built-in Django {% if %} template with the new smart one.</p>
<p><em>7 May 2009</em>: Was asked about whether it handles combination of and/or. It does, added a test to show it. I actually like how Django doesn't let you do this, but I'm not going to confuscate my code for a restriction like this.</p>
<p><em>15 June 2009</em>: Fixed up a bug with boolean precedence (x or x == 0 was being parsed as (x or x) == 0 instead of x or (x == 0)). Add some extra test cases, including some for invalid cases.</p>

Вопрос полезен? Да0/Нет0

Ответы (17):

Ответ полезен? Да0/Нет0

Just a test.

Ответ полезен? Да0/Нет0

It's nice that in Django 1.3 the builtin if tag can already do all this. :)

Ответ полезен? Да0/Нет0

boralyl: while {% if x %} and {% if x != False %} are often the same, there are situations where you want to distinguish between False, None, [] and ''. I'm trying to figure out a workaround now.

Ответ полезен? Да0/Нет0

I am not even able to use it. Firebug throws the following error: "'smart_if' is not a valid tag library: Template library smart_if not found, tried django.templatetags.smart_if,django.contrib.admin.templatetags.smart_if"

I don't understand, I followed your instructions on top. What am I doing wrong???

I am not on Django 1.2 yet, since our ec2 instance only has 1.1, otherwise I wouldn't be using this plugin.

Ответ полезен? Да0/Нет0

This is now part of Django 1.2, so you don't need to use it there.

Ответ полезен? Да0/Нет0

maplye: The better way to do the above is: {% if user.is_superuser %}...{% endif %}

Ответ полезен? Да0/Нет0

don't not support: {% if user.is_superuser != False %}

Ответ полезен? Да0/Нет0

ok i fixed

In you doc about use u have {% load smartif %} and need to fix to {% load smart_if %}

Ответ полезен? Да0/Нет0

i was looking for this, but i have error:

'smartif' is not a valid tag library: Could not load template library from django.templatetags.smartif, No module named smartif


Ответ полезен? Да0/Нет0

Excellent! Although, would need EQ, NE, LT, GT, LE, GE, <> operators too.

Ответ полезен? Да0/Нет0

{% if a > b and a < b %} works now (I would have fixed sooner if I was notified of new comments on my snippets... I only saw this comment after I posted my fix).

I realise that {% if a > b > c %} doesn't work - but I'm not really worried. That's getting pythonistic as opposed to basic boolean logic.

Ответ полезен? Да0/Нет0

Very usefull snippets, you should really submit this for evalutation in the core.

Please also note that combinaison like {% if p > x and p < y %} or {% if x > i > y %} don't work.

Ответ полезен? Да0/Нет0

This snippet makes me happy

Ответ полезен? Да0/Нет0


Ответ полезен? Да0/Нет0

Please Please Please submit a ticket that kills the old if tag!! ;)

Ответ полезен? Да0/Нет0

What about sneaking it into trunk? C'mon committers! ;-)

Ответ полезен? Да0/Нет0

Hooray! I've been wanting one of these for literally years (even wrote part of it once before losing the code in a laptop theft). This design is exactly what I was after.