Optional query parameter

Is there a way to optionally include an external parameter in a string query?


Consider the following structure (notice the third entry’s size is empty/null)

    #     type     size
    1.    first     50
    2.    sec       76
    3.    third     -

I would like to include the size between brackets in a query text (for example a in a select field).
So it would give a list of the following:

first (50)
sec (76)

I thought a query string like the following might do the trick:

"{{ structureItem.type }} {{ structureItem.size != null ? '(' . structureItem.size . ')' : '' }}"

But apparently between the {{ }} is not actual code execution (as I thought the documentation sugested by directly mirroring/copying the php Kirby API).

So is there a way to achieve the above?

You could create a custom collection method.

Should work with structures as well. Your method would then either return the number in parens or nothing and can be used instead of size in your query string. Not tested. And make sure you are on Kirby 3.4.x

Most of the API methods, yes, but not PHP logic.

I get how that can work (and it propably will do).

Not ideal for every situation considering for the select field for example one can define a text and value query. For the value property its best to either define a ‘final’ value, or an identifiable value (like number, or in this case type).

I think this can be enhanced with some kind of orElse implementation, but I’ll leave that up to the devs to decide :smiley: Thanks for the alternative anyways!

Another option:

{{ structureItem.type }} ({{ structureItem.size.or('--') }})

So instead of no parens, you explicitely declare the value as not defined.

That seems like a nice option for single values (for structures some custom handling seems to be the only option). I’ll keep that one in mind!