How to get the key value in the block template of the builder field plugin?

Now I want to create a page where the navigation menu can be scrolled to a specified block.
just like:

<a href="#001">001</a>
<a href="#002">002</a>
...
<div id="001">001 box</div>
<div id="002">002 box</div>

for example: snippets/blocks/block.php

<div id="<?= (How to get the key of each block) ?>">
  <h3><?= $data->title() ?><h3>
</div>

hey taoguangc!

i do it like this:

i pass the key to the loop (i call it id)

	<?php # /site/templates/yourtemplate.php
	foreach($page->mybuilder()->toBuilderBlocks() as $key=>$block):
	  snippet('blocks/' . $block->_key(), array('data' => $block, 'id' => $key));
	endforeach;
	?>

then inside the block i use it like this:

<?= $id ?>

hope that helps! :slight_smile:

1 Like

You’re right! thank you!