Sorry, we don't support your browser.  Install a modern browser

Snippet slot attributes

I love the slot templating feature. In a few situations it would be great to pass data or attributes to a given slot to modify its output.

A short example:

<?php // site/templates/default.php ?>

<?php snippet('card', slots: true) ?>
  <?php slot('title', ['level' => 3]) ?>
      <?= $page->title()->esc() ?>
  <?php endslot() ?>
<?php endsnippet() ?>

In the snippet you could then retrieve your custom attributes:

<?php // site/snippets/card.php

<div class="card">
  <?php if ($title = $slots->title()) : ?>
     <h<?= $title->attr('level', 2) ?>>
       <?= $title ?> 
     </h<?= $title->attr('level', 2) ?>>
  <?php endif ?>

  <?= $slot ?>
</div>

Currently, this can only be solved by providing these types of attributes via the data variable of the snippet helper. Sometimes this requires adding namespaces/prefixes to your variables to avoid collisions. I think slot attributes would make these types of customization much easier. Furthermore, the implementation of this feature should be straightforward and backwards-compatible.

7 months ago