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

Тел: +7 965 3737 888

503

Просмотров

17

Ответов

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
file_5122.py(12.9Кб)
None

Ответы (17):

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

Just a test.

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

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

Ответchadselph:21.09.2010
Ответ полезен? Да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.

Ответiganapolsky:15.07.2010
Ответ полезен? Да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.

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

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

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

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

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

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

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

ok i fixed

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

Ответasinox:13.08.2009
Ответ полезен? Да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

thanks

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

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

Ответsmileychris:14.06.2009
Ответ полезен? Да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.

Ответkrystal:09.05.2009
Ответ полезен? Да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.

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

This snippet makes me happy

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

Seriously.

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

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

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

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

Ответsimon:03.03.2009
Ответ полезен? Да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.