Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Shorthand ternary operators

I want to use shorthand ternary operators in volt, is it possible?

In PHP 5.x:

$var = $x ?: $y;

In PHP 7.x:

$var = $x ?? $y;

My usage:

$var = request.getQuery('ids') ? request.getQuery('ids') : [];

Expected but gettting error

$var = request.getQuery('ids') ?: [];


25.6k

This is volt syntax:

{% set var = request.getQuery('ids') ?: [] %}


10.3k

Syntax error, unexpected token COLON



25.6k
Accepted
answer
edited Sep '16

How about:

{% set var = request.getQuery('ids')|default([]) %}

https://docs.phalconphp.com/uk/latest/reference/volt.html#filters

As far as I can see at time of writing the volt compiler does not process elvis operator ?: or null coalescing operator ?? .



10.3k
edited Sep '16

That my answer! Great solution, thanks @baychae

I hadnt use before default filter, i like it ;)

How about:

{% set var = request.getQuery('ids')|default([]) %}

https://docs.phalconphp.com/uk/latest/reference/volt.html#filters

As far as I can see at time of writing the volt compiler does not process elvis operator ?: or null coalescing operator ?? .