pebble
pebble copied to clipboard
Elvis Operator Support
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
Hello any updates?
Couldn't you just use the 'default' filter?
{{ a | default('Hello') }}
Yeah default filter's gonna do the trick
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.
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.
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.
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.
Hi Hector, I would also like to see the Elvis operator in Pebble - it's nice, cute and short to write.
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?