Code

Opened 4 years ago

Closed 4 years ago

#14333 closed (wontfix)

Template Ternary Operator

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

Description

Hey guys new django user here, I really like what you guys are doing.
The feature request I am presenting is small but I think would be a good addition.
I think there needs to a Ternary operator in your template system.

I know you can do

{% if <var1> %}<var1>{% else %}<var2>{% endif %}

Seems a little overkill for just needing to display one variable over the other. My suggestion is create a ternary operator.

{% <var1> ? <var1> : <var2> %}

It could be cut down even more by this.

{% <var1> ? <var2> %}

I think this is a great way to reduce code markup and make it more readable.
Thank you for your time.
Scottix

Attachments (0)

Change History (1)

comment:1 Changed 4 years ago by russellm

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to wontfix
  • Status changed from new to closed

Oh, for the love of all that is holy, no. The ?: ternary operator is one of the worst things that C visited on the world. Even if Django's template language *was* a programming language, I'd be -1 to any attempt to introduce this. And it isn't, which gives us another reason.

And, for the record, the second form is really just the {{|default}} filter.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.