On 01/03/17 01:53, Tim Martin wrote:

On Tuesday, 28 February 2017 13:39:21 UTC, Luke Plant wrote:


    On 28/02/17 15:24, Marten Kenbeek wrote:
    What about adding a filter |definedthat returns True if a
    variable is defined, False otherwise? It may not solve any
    problems when it's left implicit, but at least it allows users
    the explicitly define the behaviour for non-existing template
    variables, and it makes the intention of the template code
    crystal clear. And of course it's fully backwards compatible. The
    drawback is perhaps the resulting verbosity of if statements:

        {% if user.role|defined and roles.staff|defined and user.role
    == roles.staff %}
            [sensitive information here]
        {% endif %}


    The only way to do it would be to hard code this filter into the
    template engine, because otherwise the 'defined' filter runs too
    late to know whether the variable is defined.  This is really
    hacky and magic.



Is that true? In my patch, the undefined variable gets expanded into an UndefinedVariable sentinel object, which can be detected by the defined modifier, and can compare == to None (or not, if you wish). Other than the result of the 'is' operator, I think you can make this fully backward compatible if you choose. Whether or not this is desirable is another question, of course.

I was talking about with the current behaviour of template engine i.e. without changing how undefined variables are handled. For this to work as you proposed, you would have to be passing the UndefinedVariable object into these filters. Having checked out your branch, I can see that is what you are doing.

This is a really big backwards incompatibility, as far as I can see. It means that any filter may now get passed `UndefinedVariable` instead of being passed `None` or string_if_invalid. So for example, the following template behaves oppositely with your patch, if variable 'missing' is undefined:

{% if missing|yesno == "maybe" %}true{% else %}false{% endif %}

There are likely many other filters that will behave differently e.g.:

{{ missing|add:"x" }}

Without the patch, if 'missing' is undefined this returns "x", but with the patch it returns "". It's not just builtin filters I'm thinking about, it's everyone else's that would also have to be made aware of this new false-y value that it will start receiving. This is a really big backwards incompatible change, I don't see how we could justify this. If we were starting from scratch it would be another matter.

Luke

--
You received this message because you are subscribed to the Google Groups "Django 
developers  (Contributions to Django itself)" 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 https://groups.google.com/group/django-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/fb53a533-9e83-476c-a20d-6bc7953b928c%40cantab.net.
For more options, visit https://groups.google.com/d/optout.

Reply via email to