For each line in kirbytext …

Hey folks,

I’m trying to dynamically print some <p>’s from kirbytext.

kirbytext (in panel) are words or sentences with just a line break (hitting the enter key).

In my php file I want to do something like …

for each line in kirbytext do
  <p>some text I want to repeat in every p-tag $word-from-each-line</p>

Hope someone can help. : )

Hello @fooness !
Is there any specific reason you want to do that?
I’m curious :slight_smile:

Hey @Thiousi,

hm I’m having sentences which all start with the same words and I thought it would be good to just write this sentence once in the php-string and append all keywords which build the end of that sentence dynamically iterating line by line from kirbytext … does that make sense? = )


for each $line in kirbytext field from panel; do
  <p>My sentence foo bar what ever et cetera <?php echo $line->text() ?></p>

You can probably do that with a kirbytext post filter and some regex voodoo:

however, the words should then be rendered as paragraphs.

Oh I didn’t think it would be “that” complicated.

Well, you can’t iterate through a textarea field. An alternative would be to use a tags field, a list field or something similar, which would then give you an array you can loop through and prepend your string.

1 Like

May I suggest you use another field?
A structure field perhaps where each line is a structure entry. It’s super easy to loop through entries in a structure field afterwards!
Or a tag field if that suits your use case (not very nice for long sentences I guess)

Edit: texnixe is too fast :fast_forward:

@texnixe Does this still work? I save the list.php and template.php to site/fields/list/ and added the field to my blueprint … but hm. I’m not sure about the following part, what does it mean? “The content will be YAML-structured. Inside the template, the field therefore has to be decoded as an array using $page->fruits()->yaml().”

I’m seeing one field with the placeholder and the titel above, I can type in the field but nothing more.

Uh, now it works. Seems I broke the script while adding a semicolon to list.js at the end of the second line of:

      handle: ".sortable-handle"

You get the items in the list like this:

$items = $page->name_of_field()->yaml();
foreach($items as $item): ?>
  <p><?= $your_string . " " . $item ?></p>
<?php endforeach ?>

Great! : ) Thank you, @texnixe.

Could you please tell me two things? What does <?= mean, what’s a not abbreviated form of it?

And how would I rewrite this php code to use a dynamic array of words from the list I just created? So I don’t have to type the words there again.

$words = array("one", "two", "three", "four", "..."); echo $words[rand(0, sizeof($words) - 1)]; ?>

<?= is a short tag for <?php echo.

$words = $page->name_of_field()->yaml();
$index = rand(0, sizeof($words) -1);
echo $word[$index];

Hilarious. : )

One last tiny question, where are field-names saved? E.g. if I’d like to rename name_of_field() …

Thank you so much and have a great evening, @texnixe.

1 Like

They’re defined in your blueprints and saved in the content files using that blueprint :slight_smile:

You don’t even need the blueprints though.

Just adding a line in a content file like that:


fieldname: content of your field


otherfield: content of other field

Makes the kirby function page->fieldname() available !

Oh for sure, in the contents folder … sorry heh. : )

This new approach of @Thiousi is also interesting! In this case I wouln’t be able to add those via the panel, I suppose, but good to know for other things I planed! Thank you.

@fooness: You don’t necessarily have to use the panel, that is optional. If you create your files manually, you can also later edit them via the panel, provided you have created the blueprint with the corresponding fields.

You could actually use a textarea with one word per line:

$items = $page->name_of_field()->split('\n');

But structured content is definitely more robust here.