Access variables from nested snippet

Hi there,

Using the Kirby-builder, I’m having trouble figuring out how to access values in a nested snippet from a level up within an array structure.

My file structure looks like this:

blueprints/fields/builder/contentblock.yml
blueprints/fields/builder/nested/textblock.yml
snippets/builder/contentblock.php
snippets/builder/nested/textblock.php

I want to be able to define some variables once in contentblock.yml and use them inside the textblock.php snippet in a foreach loop.

My blueprint structure looks something like this:

label: Content Block
fields:
  builder_nested:
    label: Content body 
    type: builder
    fieldsets:
        button:
          extends: fields/builder/nested/button
        copy:
          extends: fields/builder/nested/textblock
  sectionbghex:
    label: Section BG colour
    type: text
  copy_hex:
    label: Section Text Hex
    type: text

The foreach loop in contentblock.php works fine:

<?php foreach($data->builder_nested()->toBuilderBlocks() as $block): ?>
<?php snippet('builder/nested/' . $block->_key(), array('data' => $block)) ?>
<?php endforeach ?>

Inside textblock.php, I can access the nested values under builder_nested, but not sectionbghex nor copy_hex

I’ve tried passing a variable into the snippet like this:

<?php snippet('builder/nested/' . $block->_key(), array('data' => $block), array('copy_hex' => $data->copy_hex())) ?>

but this throws an error:

Argument 3 passed to snippet() must be of the type boolean, array given, called in …/site/snippets/builder/contentblock.php on line 16

I‘m a relative newb to this and most of my php & Kirby knowledge has been carved out by trial and error, so it could well be that this is a really basic principle that I’ve missed – sorry about that :slight_smile:

You have to pass the variable in the same array, not create multiple arrays:

['data' => $block, 'copy_hex' => $data->copyhex()]

Okay, I see, thank you – the issue that now arises is that the values in the builder-nested array no longer seem to be available. I’m getting an error (Call to a member function copy_type() on null) on the first if statement in my textblock.php snippet.

Is it because the snippet now has two variables passed to it, and the array structure has changed?

I don’t have enough information, the above code snippets don’t contain a call to copy_type() nor an if statement.

Sorry, I was trying to keep things simple! Here’s the blueprint for the nested copy.yml file,

label: Copy
fields:
  copy_text:
    width: 1
    label: Text
    type: textarea
  copy_type:
    width: 2/3
    label: Type
    type: select
    default: regular
    options:
      none: none
      intro: Intro
      regular: Wide
      more: Extra indentation
      small: Smaller Terms Copy

and the related snippet:

<?php if ($data->copy_type() == 'more'): ?>
  <tr>
    <td>
      <table border="0" cellpadding="0" cellspacing="0" width="100%">
        <tr>
          <td class="em_side" width="40">
            &nbsp;
          </td>
          <td valign="top">
            <table border="0" cellpadding="0" cellspacing="0" width="100%">
              <tr>
                <td align="center" class="em_black em_lh18" style="font-size:14px; line-height:22px; font-family:Arial, Helvetica, sans-serif; color:<?= $copy_hex() ?>; letter-spacing:1px;">
					<?= $data->copy_text()->kirbytext() ?>
                </td>
              </tr>
            </table>
          </td>
          <td class="em_side" width="40">
            &nbsp;
          </td>
        </tr>
      </table>
    </td>
  </tr>

<?php else: ?>
  <tr>
	  <td align="center" class="em_black em_lh18" style="<?php if ($data->copy_type() == 'small'):  ?>font-size: 12px; line-height: 16px;<?php elseif ($data->copy_type() == 'intro'): ?>font-size: 22px; line-height: 28px;<?php else: ?>font-size: 14px; line-height: 22px;<?php endif ?>; font-family:Arial, Helvetica, sans-serif; color:<?php echo $copy_hex ?>; letter-spacing:1px;">
      <?= $data->copy_text()->kirbytext() ?>
    </td>
  </tr>
<?php endif ?>