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
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:
'yaar' => 'Yaar',
'status' => 'Status'
An then call the value by its key.
// get the array in a template
$infomap = c::get('info.map');
// 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; ?>