Using a snippet in a block

I’m trying a create a hero section that is fixed on some templates (like the blog and blog posts page) but that can also be an optional block for a page that use a builder type template.

When it’s fixed, it would show the page title and sometimes a cover image (if set)
When added as a block, the user has the ability to add their own title and choose a background image (optional).

The following code does not throw up and errors (I have debugging on) but the block I’m making does not appear on the site when I add it via the cms.

site>snippets>pageHero.php

<?php $isUpdate = $page->parent('updates'); ?>

<?php if ($isUpdate): ?>
  <h5><?= $page->parent()->title()->esc() ?></h5>
  <h1><?= $page->title()->html() ?></h1>

  <?php if ($page->cover()->isNotEmpty()): ?>
      <?php foreach ($page->cover()->toFiles() as $image): ?>
    <?php
    $sizes = "(min-width: 1200px) 50vw,
              (min-width: 900px) 33vw,
              (min-width: 600px) 50vw,
              100vw";
    ?>
    <picture>
      <source srcset="<?= $image->srcset('webp') ?>" sizes="<?= $sizes ?>" type="image/webp">
      <img
          class=""
          alt="<?= $image->alt() ?>"
          data-src="<?= $image->resize(300)->url() ?>"
          data-srcset="<?= $image->srcset() ?>"
          sizes="<?= $sizes ?>"
          width="<?= $image->resize(310)->width() ?>"
          height="<?//= $image->resize(500)->height() ?>">
    </picture>
    <?php endforeach ?>
  <?php else: ?>
    <!-- no placeholder image -->
  <?php endif ?>

<?php elseif (isset($block)): ?>
  <?php if ($block->background()->isNotEmpty()): ?>
    <?php foreach ($block->background()->toFiles() as $image): ?>
      <?php
      $sizes = "(min-width: 1200px) 50vw,
                (min-width: 900px) 33vw,
                (min-width: 600px) 50vw,
                100vw";
      ?>
      <picture>
        <source srcset="<?= $image->srcset('webp') ?>" sizes="<?= $sizes ?>" type="image/webp">
        <img
          class=""
          alt="<?= $image->alt() ?>"
          data-src="<?= $image->resize(300)->url() ?>"
          data-srcset="<?= $image->srcset() ?>"
          sizes="<?= $sizes ?>"
          width="<?//= $image->resize(310)->width() ?>"
          height="<?//= $image->resize(500)->height() ?>">
      </picture>
    <?php endforeach ?>
  <?php else: ?>
    <!-- no background image -->
  <?php endif ?>
  <h1><?= $block->heading() ?></h1>
<?php endif ?>

site>snippets>blocks>pageHero.php

<?php snippet('pageHero') ?>

$page is not defined when in a block context, so that would fails, you need to check if $page is defined in the same way as you check if $block is defined. Also, in your block snippet, you need to pass the $block variable to the hero snippet.

site>snippets>blocks>pageHero.php

snippet('pageHero', ['block' => $block]);

That did the trick, thanks!