amphtml icon indicating copy to clipboard operation
amphtml copied to clipboard

amp-bind: Support shortened ternary syntax

Open kmh287 opened this issue 9 years ago • 3 comments

Right now amp-bind supports the ternary operator

A ? B : C

which returns B if A is truthy and C otherwise. I would like to expand this to support the following:

A ?: B

which returns A if A is truthy and B otherwise.

This is particularly useful for expressions that have fallbacks when a variable isn't defined in a page's amp-state. (undefined variables are always null) So:

<p>You have selected <span [text]="selection ? selection : 'nothing'">nothing</span></p>

could be shortened to

<p>You have selected <span [text]="selection ? : 'nothing'">nothing</span></p>

@choumx

kmh287 avatar Mar 24 '17 18:03 kmh287

Actually Dima brought this up too (sometimes called the Elvis operator).

|| works the same way and seems more commonly used, though couldn't hurt I suppose.

dreamofabear avatar Mar 24 '17 18:03 dreamofabear

Looks like ?: isn't natively supported in JS. Do you think it's worth breaking consistency here?

dreamofabear avatar Mar 24 '17 20:03 dreamofabear

Since || provides the same functionality, I could go either way.

kmh287 avatar Mar 24 '17 20:03 kmh287