Hi,
thanks for your input. Unfortunatelly this would fail for int(0):
{'value': 0} => <input value>
Gregor
2014-04-10 11:41 GMT+02:00 Tino de Bruijn <[email protected]>:
> Wouldn't this be easier?:
>
> {'required': "", 'name': 'fieldname'} => <input required name="fieldname"
> />
>
> {% for name, value in attrs.items %} {{ name }}{% if value %}="{{ value
> }}"{% endif %}{% endfor %}
>
>
> Tino
>
>
> On Wed, Apr 9, 2014 at 11:53 PM, Gregor Müllegger <[email protected]>wrote:
>
>> Hi,
>>
>> I recently had the need to check for "value is not True" (as an identity
>> check, not
>> on equality) in django-floppyforms. The template should generate the HTML
>> attributes for an HTML element from a python dict, but leave out the
>> attribute
>> value if the dict's value is True. Like:
>>
>> {'required': True, 'name': 'fieldname'} => <input required
>> name="fieldname" />
>>
>> If there would be an "is" identity check in the Django template language
>> it
>> would look like:
>>
>> {% for name, value in attrs.items %} {{ name }}{% if value is not
>> True %}="{{ value }}"{% endif %}{% endfor %}
>>
>> But since there is none, the if case looks now like this:
>>
>> {% if value|stringformat:"s" != "True" or value != 1 %}
>>
>> See:
>> https://github.com/gregmuellegger/django-floppyforms/blob/master/floppyforms/templates/floppyforms/attrs.html
>>
>> The template previously checked with {% if not value %}, but that would
>> break if
>> you pass in the integer 1. Since "True == 1", but "True is not 1".
>>
>>
>> I do get why there is no "is" comperator in the DTL yet. It might be
>> confused
>> with the equality check by designers that don't know the underlying
>> concepts of
>> equality and identity. And I think that's totally reasonable.
>>
>> However I still want to bring this up since there is this real usecase I
>> described above. Ofcourse I could introduce a custom filter or tag to
>> scratch
>> that itch, but IMO this is something very fundamental that I would expect
>> to have
>> as a python developer.
>>
>> So here are a few solutions I can think of:
>>
>> 1. Have an "is" and "is not" comperator in the {% if %} tag. Could lead
>> to confusion.
>>
>> 2. Have a "===" and "!==" comperator in the {% if %} tag. This is
>> something nearer to
>> Javascript than python and makes it clear that it's different to ==
>> but would
>> still confuse someone who did't read the DTL documentation.
>>
>> 3. Have a "is_identical" builtin filter. So it would be {% if not
>> value|is_identical:True %}.
>>
>>
>> My favourite is option 3. It's very readable and doesn't introduce
>> possible
>> confusions for non-python programmers.
>>
>> What is your opinion on this?
>>
>>
>> Gregor
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django developers" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>> To post to this group, send email to [email protected].
>> Visit this group at http://groups.google.com/group/django-developers.
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-developers/CALjSGCPsE3TTr2zzkUZboADpSOCymCue1HVC_SqB6mLMfpfwWA%40mail.gmail.com<https://groups.google.com/d/msgid/django-developers/CALjSGCPsE3TTr2zzkUZboADpSOCymCue1HVC_SqB6mLMfpfwWA%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>> For more options, visit https://groups.google.com/d/optout.
>>
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django developers" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/django-developers.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-developers/CANQFsQDXR7FcfdgaF4w6G_9uaS7e-W_Y7FnxG3%2BxFTR4h4n7OA%40mail.gmail.com<https://groups.google.com/d/msgid/django-developers/CANQFsQDXR7FcfdgaF4w6G_9uaS7e-W_Y7FnxG3%2BxFTR4h4n7OA%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
> For more options, visit https://groups.google.com/d/optout.
>
--
You received this message because you are subscribed to the Google Groups
"Django developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-developers/CALjSGCOwL%3DvAD%2Bj3vRyNBFTjFOonHRzz9PebgSbBTuZsiQSERw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.