Content of list items are not visible

Hi,
And a happy new year.

I created a field with list items and the format:

Appointments:
-
  MM, 01.02.2020, 18:30 Uhr
-
  MM, 02.03.2020, 18:30 Uhr
-
  MM, 03.05.2020, 18:30 Uhr
-
  MM, 04.06.2020, 18:30 Uhr

The following code shows the items als UL, but without the content above.

<div class="appointment">
      <ul>
        <?php foreach ($page->appointments()->toStructure() as $item): ?>
          <li>Test <?= $item->kt() ?></li>
        <?php endforeach ?>
      </ul>
 </div>

The prefix “Test” was a test, that standard text and the right item amount will be shown. But not the field content.

Any idea?

Thank you

Looks like you created that file manually? Your items should have a key:

Appointments:
-
  appointment: MM, 01.02.2020, 18:30 Uhr
-
  appointment: MM, 02.03.2020, 18:30 Uhr
-
  appointment: MM, 03.05.2020, 18:30 Uhr
-
  appointment: MM, 04.06.2020, 18:30 Uhrs  

Then in template:

  <div class="appointment">
    <ul>
      <?php foreach ($page->appointments()->toStructure() as $item) : ?>
        <li>Test <?= $item->appointment()->kt() ?></li>
      <?php endforeach ?>
    </ul>
  </div>

Thank you,
It solve my problem. But is this the right cosmetic way, to consider in fields a list and to show this list later in HTML?

That’s not cosmetic, it’s the way a structure field works, because it can have multiple subfields. If you want a simple markdown list, you would have to add it like this

Appointments:

- item 1
- item 2
- item 3

Then in your template:

<?= $page->appointments()->kt() ?>

That would then generate your list markup.

If you want to keep your structure as is without a key, you can use yaml() instead of toStructure()

<div class="appointment">
    <ul>
      <?php foreach ($page->appointments()->yaml() as $item) : ?>
        <li>Test <?= $item[0] ?></li>
      <?php endforeach ?>
    </ul>
  </div>
1 Like

Perfect.