pebble icon indicating copy to clipboard operation
pebble copied to clipboard

Elvis Operator Support

Open lmajano opened this issue 6 years ago • 9 comments

Is the elvis operator supported now in pebble?

Example to avoid nullness

Let's say a = null;

{{ a ?: "Hello "}}

Then Hello will be printed since a is null

lmajano avatar Mar 16 '18 20:03 lmajano

Hello any updates?

lmajano avatar Mar 22 '18 19:03 lmajano

Couldn't you just use the 'default' filter?

{{ a | default('Hello') }}

nicky9door avatar Apr 08 '18 19:04 nicky9door

Yeah default filter's gonna do the trick

ebussieres avatar Apr 08 '18 19:04 ebussieres

Thank you for the responses. I know I can use the piped default filter. However, the elvis construct is less verbose, semantic and used in all major languages.

{{ a ?: "Luis" }}

vs

{{ a | default( "Luis" ) }}

Same goes for the ternary operator. Instead of using an if/else tag we can have a more semantic construct.

lmajano avatar Apr 09 '18 14:04 lmajano

Why is ?: more semantic than | default? Because other languages use it? IMHO this doesn't need to be a programming language (in fact, the less code you get into a template, the better).

Having said that, Twig supports the elvis operator. But... I don't like the idea of having 2 completely different ways of doing the same. Honestly, not sure what to say.

hectorlf avatar Apr 26 '18 00:04 hectorlf

Well you can use the ternary operator and an if else. So that’s two ways to do the same thing as well.

Luis Majano CEO Ortus Solutions, Corp www.ortussolutions.com P/F: 1-888-557-8057


From: Héctor López [email protected] Sent: Wednesday, April 25, 2018 7:40:44 PM To: PebbleTemplates/pebble Cc: Luis Majano; Author Subject: Re: [PebbleTemplates/pebble] Elvis Operator Support (#324)

Why is ?: more semantic than | default? Because other languages use it? IMHO this doesn't need to be a programming language (in fact, the less code you get into a template, the better).

Having said that, Twig supports the elvis operator. But... I don't like the idea of having 2 completely different ways of doing the same. Honestly, not sure what to say.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHubhttps://github.com/PebbleTemplates/pebble/issues/324#issuecomment-384476473, or mute the threadhttps://github.com/notifications/unsubscribe-auth/AAIXl0wwoZtv5mc32AxNjtTgufW4bBXUks5tsReMgaJpZM4Sua8c.

lmajano avatar Apr 26 '18 03:04 lmajano

To be honest, I had completely forgotten that Pebble had a ternary operator. That doesn't mean I have to agree, though! Mitchell was the sole owner until a month ago.

But you're right, supporting elvis should be straightforward. Let me have a look.

hectorlf avatar Apr 26 '18 18:04 hectorlf

Hi Hector, I would also like to see the Elvis operator in Pebble - it's nice, cute and short to write.

JosefBoukal avatar Apr 26 '18 19:04 JosefBoukal

Did a quick test and found something worrying: our current definition of truth might be an impediment. Right now, in the ternary op, anything that doesn't coerce to boolean will blow up with an exception (null in this case coerces to false). If we were to offer the elvis without changing the ternary, these two would behave differently in regards to 'test' evaluation:

test ? '' : 'no' test ?: 'no'

Honestly, I don't like the idea. What's worse, trying to fix the evaluation of truth values would be a possible breaking change, that I don't see happening in the 2.X branch...

Thoughts?

hectorlf avatar Apr 26 '18 21:04 hectorlf