Opened 9 years ago
Closed 9 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)
follow-up: 2 comment:1 by , 9 years ago
comment:2 by , 9 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 , 9 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 , 9 years ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
Let's reopen if you can get consensus on the mailing list. Thanks!
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.