Opened 8 years ago

Closed 8 years ago

#25742 closed New feature (wontfix)

"default_if_not_set" template filter

Reported by: Ramez Issac Owned by: nobody
Component: Template system Version: 1.8
Severity: Normal Keywords: template default filter
Cc: ramezashraf@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Working with template, sometimes i want to make a snippet to be included and control it via context variables.

{% include 'snippet.html' my_var=False %}

in the snippet.html

{# want my_var to default to True #}
<div> 
{% if my_var %}
    {# Do so and so #}
{% endif %}
</div>

Problem is that we can't set a default value for those variables

my_var|default filter will evaluate the variable, if it's False (which might be the desired), the tag will change it
On the other hand, default_if_none filter , will have no effect as unassigned context variables are evaluated as empty string (not None)

Proposal:
introduce default_if_not_set filter.

I can do it if accepted.
Regards

Change History (4)

comment:1 by Collin Anderson, 8 years ago

I feel like I should point out that if you renamed my_var to not_my_var (or somehow rephrase your var), you could have it default to False, and then your if statement would work correctly.

in reply to:  1 comment:2 by Ramez Issac, 8 years ago

Replying to collinanderson:

I feel like I should point out that if you renamed my_var to not_my_var (or somehow rephrase your var), you could have it default to False, and then your if statement would work correctly.

Sure.
But it's not helping readability when my actual variable name dont_include_that_div (default to False) instead of include_that_div (default to True)

comment:3 by Tim Graham, 8 years ago

To help make a decision, I'd suggest raising the issue on the DevelopersMailingList. Is it currently implemented in a popular third-party template filter library? Does it meet the 80% use case test for sites? I think it creates more complexity in templates which the Django Template Language tries to avoid, but I haven't thought about the issue much.

comment:4 by Tim Graham, 8 years ago

Resolution: wontfix
Status: newclosed

Let's reopen if you can get consensus on the mailing list. Thanks!

Note: See TracTickets for help on using tickets.
Back to Top