Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#31011 closed New feature (duplicate)

A new template tag for ternary operations?

Reported by: Remy Owned by: nobody
Component: Template system Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I am aware that this issue was already pointed out here, but it was 9 years ago, so I allowed myself to bring it back again.

HTML easily gets complex and nested, and in many case you don't want to add to this complexity and you need to add inline logic but you can't do that in Django without a lot of verbosity.

Django's PHP competitor Laravel manages template with something called Blade, in which inserting a one-line condition looks like:
{{ condition ? var1 : var2 }}
... which is the exact same syntaxe as many major JavaScript-based frontend frameworks (Vue / React...).

Rails's templating engine ERB also provides a similar syntaxe:
<%= condition? ? var1 : var2 %>

I also remember one of my projects in Angular 1, where I was even able to use a filter on the output of it:
{{ condition ? date1 : date2 | dateFilter }}
(and also have space around the filter for better readability, but that's another topic... Sparse is better than dense)

In Django that would translate in:

{% if condition %}{{ date1|date:'c' }}{% else %}{{ date2|date:'c' }}{% endif %}

That is... pretty verbose. Especially if you replace those placeholder vars with actual (often longer) ones. In an actual project you often find yourself violating in many ways the Zen of Python (flat vs nested, readability, etc).

The other tools we have at our disposal is :

  • The filter |default:'' works fine in a lot of cases, but sometimes you just need to check for a third parameter.
  • The filter |yesno:"yes,no" however this works only for displaying strings.

Possible solution

Well, the point is not to say that Django should allow the exact same syntaxe as other languages, but at least I think it should allow some sort of natively supported ternary operation.

I suggest to keep the pythonic way of writing a ternary operation and implement it within Django template. Example:
{% ternary var1 if condition else var2 %}
Currently:
{% if condition %}{{ var1 }}{% else %}{{ var2 }}{% endif %}

Given the arbitrary "ternary" wording, this is {% ternary if else %} vs {% if %}{{ }}{% else %}{{ }}{% endif %}. 43 chars -> 25 chars (about 43% reduction).

Final thoughts

If this is to be resolved as wontfix again, would it be possible to at least explain a clear and practical reason as for why we don't want to include a native ternary operation in Django templates, where virtually every other languages and frameworks do include one? (python, JavaScript, Rails, Laravel ...)

Change History (2)

comment:1 by Mariusz Felisiak, 5 years ago

Resolution: duplicate
Status: newclosed

Please follow triaging guidelines with regards to wontfix tickets and don't open duplicates.

Duplicate of #14333.

comment:2 by Mariusz Felisiak, 5 years ago

Component: UncategorizedTemplate system
Version: 2.2master
Note: See TracTickets for help on using tickets.
Back to Top