Ndugu
January 30, 2020, 12:11am
1
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?
1 Like
texnixe
January 30, 2020, 12:14am
2
Ndugu:
<?= $page('demo')->text->kirbytext() ?>
Not quite:
<?= page('demo')->text()->kirbytext() ?>
But better check if the page exists:
<?= ($page = page('demo')) ? $page->text()->kt() : '' ?>
Ndugu
January 30, 2020, 12:22am
3
So, page
is 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!!!
texnixe
January 30, 2020, 12:23am
4
texnixe
January 30, 2020, 12:24am
5
The whole construct is called a ternary operator, yes, shorthand for if clause. https://davidwalsh.name/php-shorthand-if-else-ternary-operators
Ndugu
January 30, 2020, 12:25am
6
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?
texnixe
January 30, 2020, 12:27am
7
Let’s change it to
<?= ($p = page('demo')) ? $p->text()->kt() : '' ?>
Ndugu
January 30, 2020, 12:31am
8
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.
Ndugu
January 30, 2020, 1:56pm
12
Ooooh… Groschen dropping … of course! So I could use any $something = page('somePage')) ?
Ndugu
January 31, 2020, 3:22pm
15
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.
texnixe
January 31, 2020, 4:10pm
16
Ndugu:
<?= ($spl = page('spenden')) ? $spl->spenden()->toStructure() : '' ?>
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 ?>
Ndugu
February 1, 2020, 4:53am
17
texnixe:
: null;
What does that mean or where can I find an explanation in the docs?
PS: Can’t live without you.
texnixe
February 1, 2020, 10:26am
18
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 ?>