Conditions in query language?

Is it possible to have conditions in the blueprint queries? For example, in the panel I’d like to output the menu title of a page if it exists/isn’t empty, otherwise the page title.

Try the or() method:

info: "{{ page.menutitle.or(page.title) }}"

Oh, so simple. Thanks. :slight_smile:

Should this work with custom fields? I am using kirby locator and need to show page info on the panel.

# This yields no result
info: "{{ page.location.yaml.city.or(page.location.yaml.country) }}"

# This works
info: "{{ page.location.yaml.city }}"

Any help appreciated :pray:

Use a page model instead. or() is a field method so won’t work in this context

Thx @texnixe, that works perfectly!
Here the code in case anyone might need something similar.

// site/plugins/page-models/index.php

class StoryPage extends Page {
    public function locationText() {
    $loc = $this->location()->yaml();
    
    if (isset($loc["city"])) 
      return $loc["city"];

    elseif (isset($loc["country"]))
      return $loc["country"];

    elseif ($this->locationTextOverride()->isNotEmpty()) 
      return $this->locationTextOverride()->value();

    else return "—";
  }
}

Kirby::plugin('my/pageModels', [
  'pageModels' => [
    'story' => 'StoryPage',
  ]
]);

And in the template:

info: "{{ page.locationText }}"
1 Like

I’m digging this thread up again because I have a related question now: I need to display a separator, like: "{{ page.field1 }} – {{ page.field2 }}", but only if the first field isn’t empty (otherwise just display field 2). Is there anything simple for this case? The common field methods don’t seem to do here, as far as I can see.

I’d create a custom method, such conditions don’t work in query language