Text Formating inside a structured Field

How would I apply the standart kirbytext Textformating in a structures Field?

  label: Sample
  type: structure
  entry: >
    <b>Cost</b>: {{cost}}
      label: Cost
      type: textarea
      buttons: true
<?php foreach(yaml($page->samplestructure()) as $struc): ?>
  <?php echo $struc['cost']; ?>
<?php endforeach ?>

My Problem right here is that even when my Text is marked as bolt or italic it will not be formated to the standard kirbytext. Also adding the “->kirbytext()” to the echo wont do it (<?php echo $struc['cost']->kirbytext(); ?>)

1 Like

You can use the kirbytext() helper.

<?php foreach(yaml($page->samplestructure()) as $struc): ?>
  <?php echo kirbytext($struc['cost']); ?>
<?php endforeach ?>

The yaml() methods returns an array. Each element of an array is just a string, not a field object, so you can’t use a field method like kirbytext() on it.

Thanks alot for your help.

This is exactly why I prefer the toStructure method when working with structured fields; it allows you to work with the fluent field API as a natural extension instead of having to fall back onto clumsy PHP array constructs and the Kirby helper functions.

<?php foreach ($page->samplestructure()->toStructure() as $struc): ?>
  <?php echo $struc->cost()->kirbytext(); ?>
<?php endforeach; ?>

This should do the exact same thing (I have not tested this specific example, but I use that style almost everywhere). I find it much more readable and intuitive.

Yes, I also prefer the toStructure() in most cases. Sometimes it makes sense to use the yaml() method, though.

True. I’m not denying that there’s uses for yaml() - I’ve had some myself. But in a case like what @Timk describes, where the root of the problem is that yaml() doesn’t give him the field that he’d clearly like to have, I feel somewhat obliged to evangelize for the use of toStructure() :heart_eyes:

1 Like