Use as block snippet and as normal snippet

Hey :slight_smile:

I want to reuse my blocksnippets as “normal” snippets.
I have built this “big_image” block snippet. And i am using the snippet with the blockbuilder.

<?php if ($image = $data->image_global_data()->toFile()) : ?>
  <picture width="<?= $image->dimensions()->width() ?>" height="<?= $image->dimensions()->height() ?>">
    <?php if ($webPImage = $site->image($image->name() . '.webp')) : ?>
      <source data-srcset="<?= $webPImage = $site->image($image->name() . '.webp')->url() ?>" type="image/webp">
    <?php endif ?>

    <?php if ($jpgImage = $site->image($image->name() . '.jpg')) : ?>
      <source data-srcset="<?= $jpgImage = $site->image($image->name() . '.jpg')->url() ?>" type="image/jpg">
    <?php endif ?>

    <?php if ($pngImage = $site->image($image->name() . '.png')) : ?>
      <source data-srcset="<?= $pngImage = $site->image($image->name() . '.png')->url() ?>" type="image/png">
    <?php endif ?>

    <img alt="<?= $image->alt() ?>" title="<?= $image->title() ?>" width="<?= $image->dimensions()->width() ?>" height="<?= $image->dimensions()->height() ?>" src="<?= $image->resize(25)->url() ?>" data-src="<?= $image->url() ?>" class="lazyload">
  </picture>

  <figcaption class="image-caption ta-center">
    <?= $image->caption() ?>
  </figcaption>

<?php endif ?>
<!-- image_global_data is the name in the subfieldset --> 

But i want to use the snippet as “normal” snippet too.
I want to do sth like this:
<?php snippet('big_image', ['page()->image_via_panel()->toFile())' => $image]); ?>

How can i do this?

This here wont work, it needs to be a string denoting a variable you use in your snippet.

The snippet itself should be referenceable via snippet('blocks/snippetname')

So can you tell me how would it work? I mean i know that my approach doesnt work :smiley:
When my name in the panel is “customImage”. How would be the way that it works with the block snippet?

If the image object should be the variable:

In snippet

<?php if(!isset($image)) $image = $data->$fieldname()->toFile(); ?>

In template where you want to use that snippet

<?php snippet('blocks/big_image', ['image' => $image]); ?>

Thank you so far @pixelijn :slight_smile:

i am still doing sth wrong

any idea?

The problem now is your $data variable, according to the error message, that’s an array, not an object.