Shorter way for echoing custom fields

Dear people,

I am wondering whether there is a shorter way to do the following:

        <?php if($page->year()->isNotEmpty()) :?>
      <span>Year </span><span><?php echo $page->year() ?></span>
    <?php endif ?>
    <?php if($page->client()->isNotEmpty()) :?>
      <span>Client </span><span><?php echo $page->client() ?></span>
    <?php endif ?>
    <?php if($page->status()->isNotEmpty()) :?>
      <span>Status </span><span><?php echo $page->status() ?></span>
    <?php endif ?>

right now i am checking if a custom field (eg year) is set and if so, echoing it together with a short title. Is there a shorter way for this, maybe by the use of arrays?


We have a phrase here in the UK… “Swings and Roundabouts”… if you re-write this into an array or similar, your still going to end up with the same amount of code somewhere else, probably in a controller or something… in other words, a swing is the same effort as a roundabout, it just looks different :slight_smile:

You could move this information into a structure field, and then loop through it but just call the fields title and description rather then specifics of year, client, and status. That way when you loop through it, you don’t have to care how many there are, or what they are called, or if any don’t have a value.

Personally I would just make them all one line each and use the shorthand for echo…

<?php if($page->year()->isNotEmpty()) :?><span>Year </span><span><?= $page->year() ?></span><?php endif ?>

Then at least you just have three lines to read instead of 9.

Maybe you could also use the e() helper to make it even a bit shorter:

<?php e($page->year()->isNotEmpty(), '<span>Year </span><span>' . $page->year() . '</span>') ?>

That I think would work, but I always feel it is bad form to echo HTML tags, concatenated with code. It is less readable. (and your link to the docs seems to be wrong).

Oh yes thanks, just removed the link – somehow it’s not possible to link to the archive.

You could do it like this:

$fields = ['year', 'client', 'status'];
foreach($fields as $field): 
  <?php  if($page->{$field}()->isNotEmpty()): ?>
    <span><?= ucfirst($page->{$field}()->key()) ?> </span><span><?= $page->{$field}()->html() ?></span>
  <?php endif ?>
<?php endforeach ?>

Thank you all for the quick responses.
Eventhough texnixes solution is shorter, especially when it comes to more custom fields to echo, it is only usable when you want to display $field->key() .
I’m testing kirby3 atm. Does it have other possibilities?

Why? You can use it just as well if you only want the value?

What exactly do you want to achieve?


I was using a label ultimately.
Wouldn’t key() produce always the same output on multi-language sites?

Yes, sure. But if you want to loop through a set of given field that all use the same logic like above, using an array of the fields you need still produces the least overhead (compared to your original code).

Only makes sense if you want to output all fields using the same field method and the same html wrappers, of course, which is usually not the case.

I still don’t get what you want to know here. Kirby 3 has a lot of new possibilities…

I was vaguely formulating, I’m sorry. :slight_smile: Its just about being able to use {field}->label() which is not possible with kirby2 at least.

Yes, you now have access to all blueprints, see this cookbook recipe.

1 Like

Thanks texnixe!
I understand the idea of it, but I can’t figure out how to get the value of ‘label’ from the {$field} array.
it returns “Call to a member function label() on null”.
Thanks for your patience :pray:

Your syntax is not correct. For example, if you want to get the label of a field called text, you use the field() method and pass the name of the field as a parameter:

$label= $page->blueprint()->field('text')['label'];

returns an array, e.g.

    [type] => textarea
    [size] => large
    [name] => text
    [label] => Text
    [width] => 1/1

so you get the label via the index label

This code only applies to Kirby 3 (despite the fact that this thread is in the Kirby 2 category )

1 Like

Supercool! I did not know about indexing at all.

In Kirby 3, I added the blueprint method (indexing to the [‘label’] of each {$field} to your initial solution like so:

<?php $fields = ['year', 'client', 'stadium'];
foreach($fields as $field): 
<?php  if($page->{$field}()->isNotEmpty()): ?>
<?php $label = $page->{$field}()->key(); ?>
<span><?= ucfirst($page->blueprint()->field($label)['label']) ?> </span>
<span><?= $page->{$field}()->html() ?></span>
<?php endif ?>
<?php endforeach ?>

and it works perfectly :slight_smile: thank you so much!