Kirby Block option

Hi there, I’m trying to add a new field selection in my block template by following this doc:

Basically, I’ve duplicated the “ratio” data field in yml and PHP image block.
Users can change between 2 values but in the HTML It’s printed “auto” value instead of one of those… Any suggestion?

This is the snippet code, “ratioMobile” is the new field:

<?php

/** @var \Kirby\Cms\Block $block */
$alt     = $block->alt();
$caption = $block->caption();
$crop    = $block->crop()->isTrue();
$link    = $block->link();
$ratio   = $block->ratio()->or('auto');
$ratioMobile   = $block->ratioMobile()->or('auto');
$src     = null;

if ($block->location() == 'web') {
    $src = $block->src()->esc();
} elseif ($image = $block->image()->toFile()) {
    $alt = $alt->or($image->alt());
    $src = $image->url();
}

?>
<?php if ($src): ?>
<figure<?= Html::attr(['data-ratio' => $ratio, 'data-crop' => $crop, 'orientation' => $ratioMobile], null, ' ') ?>>
  <?php if ($link->isNotEmpty()): ?>
  <a href="<?= Str::esc($link->toUrl()) ?>">
    <img src="<?= $src ?>" alt="<?= $alt->esc() ?>">
  </a>
  <?php else: ?>
  <img src="<?= $src ?>" alt="<?= $alt->esc() ?>">
  <?php endif ?>

  <?php if ($caption->isNotEmpty()): ?>
  <figcaption>
    <?= $caption ?>
  </figcaption>
  <?php endif ?>
</figure>
<?php endif ?>
name: field.blocks.image.name
icon: image
preview: image
fields:
  image:
    label: field.blocks.image.name
    type: files
    multiple: false
    image:
      back: black
    uploads:
      template: blocks/image

  alt:
    label: field.blocks.image.alt
    type: text
    icon: title

  ratio:
    label: Ratio Desktop
    type: select
    placeholder: Auto
    width: 1/3
    options:
      16/9: "Full"
      4/6: "Contain"
    help: Leave empty to keep original aspect ratio  

  ratio_mobile:
    label: Ratio Mobile
    type: select
    width: 1/3
    options:
      landscape: "Landscape"
      portrait: "Portrait"
    
  crop:
    label: field.blocks.image.crop
    type: toggle
    width: 1/3

Thanks!

1 Like

In your blueprint you named the field

but in your template you called

with camelcase.

Try $block->ratio_mobile(), that should work.

Oh I didn’t notice… thank you!