Switch toggle - output of field values in snippet

Hi guys,
I spent quite some time finding a solution for my specific problem and since I am new to Kirby as well as PHP, I am trying to read and learn as much as I can.
Maybe you can help with one specific problem that I have.

In my page blueprint I do have the following code:

card_type:
label: card type
type: select
default: Summary with large image
  options:
    summary: Summary
    summary_large_image: Summary with large image
    app: App Link
    player: Video Link

The problem I have with that is that in the snippet template, I can’t get a proper output of the selected option into my source code.

Maybe someone can help with that.
I tried for example:

`<meta name="twitter:card" content="<?= $field = $page->blueprint()->field('card_type'); $value = $page->category()->value(); echo $field['options'][$value] ?? $value;?>" />`

but unfortunately, that did not help.

Great! Welcome to our community :wave:!

I assume you want to get the text that is assigned to the value that is stored in your options, right? So instead of outputting summary_large_image you want to print “Summary with large image”.

You already found the right documentation, but of course you cannot just put all that PHP into the content attribute, but have to get the value first, then echo that inside the content attribute.

<?php
$value = $page->card_type()->value();
$field = $page->blueprint()->field('card_type');
$value = $field['options'][$value] ?? '';
?>
<meta name="twitter:card" content="<?=  $value ?>" />

To keep your code clean, you could outsource all that stuff into a field method (but that’s maybe a bit too much if you’re just starting out with PHP and Kirby :wink: ).

Additionally, you could use an if statement to check if the field is filled, before you actually do all this.

thanks a lot for that quick solution! :slight_smile:
and of course for the warm welcome!

1 Like