#31011: A new template tag for ternary operations?
-----------------------------------------+------------------------
               Reporter:  lapinvert      |          Owner:  nobody
                   Type:  New feature    |         Status:  new
              Component:  Uncategorized  |        Version:  2.2
               Severity:  Normal         |       Keywords:
           Triage Stage:  Unreviewed     |      Has patch:  0
    Needs documentation:  0              |    Needs tests:  0
Patch needs improvement:  0              |  Easy pickings:  0
                  UI/UX:  0              |
-----------------------------------------+------------------------
 I am aware that this issue was already pointed out
 [https://code.djangoproject.com/ticket/14333 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` ...)

-- 
Ticket URL: <https://code.djangoproject.com/ticket/31011>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/052.ca708fd8d150e66303c6c3ccee08ae1a%40djangoproject.com.

Reply via email to