# Shorter way for echoing custom fields

**URL:** <https://forum.getkirby.com/t/shorter-way-for-echoing-custom-fields/12938>\
**Category:** Questions\
**Tags:** v3\
**Created:** [February 17, 2019, 8:43pm UTC](https://forum.getkirby.com/t/shorter-way-for-echoing-custom-fields/12938 "2019-02-17T20:43:21Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![fptt](https://avatars.discourse-cdn.com/v4/letter/f/8797f3/32.png) [@fptt](https://forum.getkirby.com/u/fptt)\
**Post date:** [February 17, 2019, 8:43pm UTC](https://forum.getkirby.com/t/shorter-way-for-echoing-custom-fields/12938/1 "2019-02-17T20:43:21Z")

</div>

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?

🙌

---

<div class="post-metadata">

**Author:** ![jimbobrjames](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jimbobrjames/32/5802_2.png) [@jimbobrjames](https://forum.getkirby.com/u/jimbobrjames)\
**Post date:** [February 17, 2019, 9:46pm UTC](https://forum.getkirby.com/t/shorter-way-for-echoing-custom-fields/12938/2 "2019-02-17T21:46:17Z")

</div>

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 🙂

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…

```auto
<?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.

---

<div class="post-metadata">

**Author:** ![pandorrrrrra](https://avatars.discourse-cdn.com/v4/letter/p/e480ec/32.png) [@pandorrrrrra](https://forum.getkirby.com/u/pandorrrrrra)\
**Post date:** [February 17, 2019, 10:03pm UTC](https://forum.getkirby.com/t/shorter-way-for-echoing-custom-fields/12938/3 "2019-02-17T22:03:16Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![jimbobrjames](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jimbobrjames/32/5802_2.png) [@jimbobrjames](https://forum.getkirby.com/u/jimbobrjames)\
**Post date:** [February 17, 2019, 10:06pm UTC](https://forum.getkirby.com/t/shorter-way-for-echoing-custom-fields/12938/4 "2019-02-17T22:06:29Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![pandorrrrrra](https://avatars.discourse-cdn.com/v4/letter/p/e480ec/32.png) [@pandorrrrrra](https://forum.getkirby.com/u/pandorrrrrra)\
**Post date:** [February 17, 2019, 10:15pm UTC](https://forum.getkirby.com/t/shorter-way-for-echoing-custom-fields/12938/5 "2019-02-17T22:15:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [February 17, 2019, 11:23pm UTC](https://forum.getkirby.com/t/shorter-way-for-echoing-custom-fields/12938/6 "2019-02-17T23:23:01Z")

</div>

You could do it like this:

```auto
<?php
$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 ?>

```

---

<div class="post-metadata">

**Author:** ![fptt](https://avatars.discourse-cdn.com/v4/letter/f/8797f3/32.png) [@fptt](https://forum.getkirby.com/u/fptt)\
**Post date:** [February 24, 2019, 3:26pm UTC](https://forum.getkirby.com/t/shorter-way-for-echoing-custom-fields/12938/7 "2019-02-24T15:26:00Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [February 24, 2019, 3:35pm UTC](https://forum.getkirby.com/t/shorter-way-for-echoing-custom-fields/12938/8 "2019-02-24T15:35:10Z")

</div>

> [@fptt](#):
>
> it is only usable when you want to display $field-\>key() .

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

What exactly do you want to achieve?

---

<div class="post-metadata">

**Author:** ![fptt](https://avatars.discourse-cdn.com/v4/letter/f/8797f3/32.png) [@fptt](https://forum.getkirby.com/u/fptt)\
**Post date:** [February 24, 2019, 3:41pm UTC](https://forum.getkirby.com/t/shorter-way-for-echoing-custom-fields/12938/9 "2019-02-24T15:41:21Z")

</div>

Hi!

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

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [February 24, 2019, 3:45pm UTC](https://forum.getkirby.com/t/shorter-way-for-echoing-custom-fields/12938/10 "2019-02-24T15:45:01Z")

</div>

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.

> [@fptt](#):
>
> I’m testing kirby3 atm. Does it have other possibilities?

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

---

<div class="post-metadata">

**Author:** ![fptt](https://avatars.discourse-cdn.com/v4/letter/f/8797f3/32.png) [@fptt](https://forum.getkirby.com/u/fptt)\
**Post date:** [February 24, 2019, 4:00pm UTC](https://forum.getkirby.com/t/shorter-way-for-echoing-custom-fields/12938/11 "2019-02-24T16:00:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [February 24, 2019, 4:47pm UTC](https://forum.getkirby.com/t/shorter-way-for-echoing-custom-fields/12938/12 "2019-02-24T16:47:45Z")

</div>

Yes, you now have access to all blueprints, see this [cookbook recipe](https://getkirby.com/docs/cookbook/templating/using-blueprints-in-frontend).

---

<div class="post-metadata">

**Author:** ![fptt](https://avatars.discourse-cdn.com/v4/letter/f/8797f3/32.png) [@fptt](https://forum.getkirby.com/u/fptt)\
**Post date:** [February 26, 2019, 9:08pm UTC](https://forum.getkirby.com/t/shorter-way-for-echoing-custom-fields/12938/13 "2019-02-26T21:08:13Z")

</div>

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.  
`$page->blueprint()->{$field}()->label()`  
it returns “Call to a member function label() on null”.  
Thanks for your patience 🙏

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [February 26, 2019, 9:14pm UTC](https://forum.getkirby.com/t/shorter-way-for-echoing-custom-fields/12938/14 "2019-02-26T21:14:56Z")

</div>

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:

```auto
<?php 
$label= $page->blueprint()->field('text')['label'];
dump($label);

```

```auto
$page->blueprint()->field('text')

```

returns an array, e.g.

```auto
Array
(
    [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 )

---

<div class="post-metadata">

**Author:** ![fptt](https://avatars.discourse-cdn.com/v4/letter/f/8797f3/32.png) [@fptt](https://forum.getkirby.com/u/fptt)\
**Post date:** [February 26, 2019, 10:08pm UTC](https://forum.getkirby.com/t/shorter-way-for-echoing-custom-fields/12938/15 "2019-02-26T22:08:02Z")

</div>

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 🙂 thank you so much!
