New operator in Latte: nullsafe pipe ?|
- David Grudl
- Nette Core | 8205
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
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 | 1193
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}