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 |defined that 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.
Tim
--
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/5e9949b2-11ae-466e-86ae-03f41754955f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.