Display content from page on homepage

I still don’t know how to get content from a field of a page on another page eg. the homepage.

It is just 1 Kt field on 1 page wich I want to display on the homepage.

I would do it like this:

  <?= $page('demo')->text->kirbytext() ?>

But that is obviously too stupid.

I tried a loop but that wasn’t right. Also I would think, since there is only one item, why should I use a loop?

Not quite:

 <?= page('demo')->text()->kirbytext() ?>

But better check if the page exists:

<?= ($page = page('demo')) ? $page->text()->kt() : '' ?>

So, pageis obviously not a variable, but a …kirbytag?

And this: ($page = page('demo')) ? is a shorthand if clause?

Maybe this snippet should be somewhere in the doks, like in Quicktipps or in the Kirby Templates 101.

PS: That was fast, even for your speed.
PPS: Thank you!!!

page() is a helper function: https://getkirby.com/docs/reference/templates/helpers/page

The whole construct is called a ternary operator, yes, shorthand for if clause. PHP Shorthand If/Else Using Ternary Operators (?:)

Oh, but now the kt field of the homepage is ignored and the content from that page (‘demo’) appears in another section as well. I need to make a template for that Demo page?

Let’s change it to

<?= ($p = page('demo')) ? $p->text()->kt() : '' ?>

Works. But why? Why does $page not work? Not allowed to have two that kind on a page? No, right?

It redefines the $page variable… so it actually shouldn’t be used for other pages in a template context, unless you don’t need $page for the current page.

It’s the same as if you do:

$color = 'blue';

dump($color);

$color = 'red';

dump($color);

That’s why these thingies are called variables, because they can change.

Ooooh… Groschen dropping … of course! So I could use any $something = page('somePage')) ?

Yep!

Cooool!

Now I would like to pull a list from a structure field onto the homepage. I used this

<?= ($spl = page('spenden')) ? $spl->spenden()->toStructure() : '' ?>

I have two test entries in that structure field, each with “Datum” and “Betrag”. It should be displayed as like “1.4.20 | 300€”, but it gives me this:

1

0

Oh let me show you its code:

columns:
  spendentext:
    type: fields
    fields:
      text:
        label: Text
        type: textarea
     spenden:
      label: Spenden
      type: structure
      fields:
        datum:
          label: Datum
          type: text
        betrag:
          label: Betrag
          type: text

Same if I use “number” as the type of the field “Betrag”.

So it is just counting the entries, not showing its value.

You are trying to echo the collection, which doesn’t make sense:

<?php $items = page('spenden') ? page('spenden')->spenden()->toStructure() : null; ?>

<?php if ($items and $items->isNotEmpty()) : ?>
<?php foreach ($items as $item) : ?>
    <?= $item->betrag() ?>
<?php endforeach ?>
<?php endif ?>

What does that mean or where can I find an explanation in the docs?

PS: Can’t live without you.

If the page does not exist, we set the $items variable to null, i.e. a variable without a value. That’s nowhere in the docs, but general PHP. Could have set this to false as well.

It is basically the same as

<?php

$items = null; // or false
if ($spendenPage = page('spenden') {
    $items = $spendenPage->spenden()->toStructure();
}
// rest as before

Or we can set $items to a new empty Structure collection (then we need only one condition in our if statement):

<?php $items = ($spendenPage = page('spenden')) ? $spendenPage->spenden()->toStructure() : new Structure(); ?>
<?php if ($items->isNotEmpty()) : ?>
    <?php foreach ($items as $item) : ?>
    <?= $item->betrag() ?>
    <?php endforeach ?>
<?php endif ?>