How to output the labels of structure field items?

I have a page template and a blueprint for projects. Each project has some data, like the year, status, client, etc. This is subject to change, but the basic idea will be the same.

So for the project info, I’ve set up a structure field. (By the way, is that actually the best way to do it?)

Ideally, I’d like to output every field in the structure field like this:

<?php foreach($page->info()->toStructure() as $info): ?>
    <?= $info; ?>
<?php endforeach ?>

With the output being something like:

Jaar: 2011 - 2012
Status: opgeleverd
Cliënt: familie Peeters

Where the first part is the label and the second part is the content of the field in the structure field.

But my current setup only outputs the content of the field, not its label. Is there a handy way to output the label as well?

This is my template and this is my blueprint.

There are two plugins that allow you to read the blueprints. Other than that, it is not possible to read the labels, only the field keys…

Thank you! Then for now I think I’ll just do it manually with a snippet.

What you can do, is map the field names to their labels in your config:

c::set('', [
  'yaar' => 'Yaar',
  'status' => 'Status'

An then call the value by its key.

// get the array in a template
$infomap = c::get('');
// access a single value
$year = $infomap[$field->fieldname()->key()]; 

Or, if your field names are the same as your labels, you can use the field names and uppercase them.

Example using the contactoptions field from the Starterkit:

<?php foreach(page('contact')->contactoptions()->structure() as $field): ?>
    <p><?= str::ucfirst($field->title()->key()).': '.$field->title(); ?></p>
<?php endforeach; ?>
1 Like