PebbleTemplates / pebble

Java Template Engine
https://pebbletemplates.io
BSD 3-Clause "New" or "Revised" License
1.09k stars 165 forks source link

Elvis Operator Support #324

Open lmajano opened 6 years ago

lmajano commented 6 years ago

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 commented 6 years ago

Hello any updates?

nicky9door commented 6 years ago

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

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

ebussieres commented 6 years ago

Yeah default filter's gonna do the trick

lmajano commented 6 years ago

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.

hectorlf commented 6 years ago

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.

lmajano commented 6 years ago

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 notifications@github.com 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.

hectorlf commented 6 years ago

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.

JosefBoukal commented 6 years ago

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

hectorlf commented 6 years ago

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?