New operator in Latte: nullsafe pipe ?|

David Grudl
Nette Core | 8218
+
+12
-

I added support for the nullsafe pipe operator ?| to the development version of Latte. This allows filter calls to be skipped if the value is null:

{$var?|upper}

This applies to the entire subsequent filter chain, such as

{$var?|trim|truncate:10|upper}

The nullsafe operator can now be used anywhere, not just at the beginning…

{$var|trim?|truncate:10|upper}

…in which case |trim is always called and the rest only depending on whether the return value is non-null.

I'm wondering if it would be better to allow ?| only at the very beginning, since I can't think of a case where the filter returns null, and thus using it elsewhere than at the beginning doesn't make sense.

What do you think?

dakur
Member | 493
+
0
-

I'm curious what are reasons for introducing it? Why I'd need to render null? Or maybe useful in some assignments?

But to the question, introducing it only at the beginning might be a good start and if someone comes with strong use-case, it can be added additionally.

Last edited by dakur (2024-01-18 09:07)

Pavel Kravčík
Member | 1195
+
+4
-

I fetching dataload from GoogleApiBooks to modal. User can choose search result to pre-fill form.

{if $book->volumeInfo->description}
	{$book->volumeInfo->description|truncate:50}
{/if}

More cool latte:

{$book->volumeInfo->description?|truncate:50}
dakur
Member | 493
+
+3
-

Ok, thanks. I've got a tag involved usually that's why I couldn't think of use-case.

<span class="selector" n:if="$description">{$description|truncate:50}</span>

Last edited by dakur (2024-01-18 11:03)

Šaman
Member | 2658
+
+2
-

dakur wrote:

I'm curious what are reasons for introducing it? Why I'd need to render null? Or maybe useful in some assignments?

For example here is use case. It is propably not best practise, but real example on ongoing projects.