Dynamic placeholders

I have a question regarding the new recipe for placeholders in Kirbytext (https://getkirby.com/docs/cookbook/extensions/use-placeholders).
I would like to use some logic in kirbytext and was wondering, if i could create a placeholder in the template like this:

<? echo 
    Str::template($page->text()->kt(), [
    'subpages' => $subpages
]) ?>

I would then declare $subpages as an html string:
<a href="http://www.url.com">Subpage 1</a>, <a href="http://www.url.com">Subpage 2</a>
through a foreach loop.

Now my 2 questions:

  1. is it possible to include a variable into the function above (the one that includes the placeholders)
    &
  2. how can i write a string from a foreachloop so i can create this html to be a placeholder?

additional question: is there maybe already a simpler way to have this kind of logic in kirbytext? how can i call the subpages of a page through kirbytext?

One way of doing this.

<?php 
$urls = $page->children()->pluck('url');
$pages = $page->children()->pluck('title', ',');

function getLinks($n, $m)
{
    return '<a href="'. $n .'">'  . $m . '</a>';
}
$result = array_map('getLinks', $urls, $pages);
$subpages = implode(', ', $result);

echo 
    Str::template($page->text()->kt(), [
    'subpages' => $subpages
]);

You could also loop through the subpages, add the links to an array, then implode.

Wow! works like a charm. As always: thanks a lot!!!
And just like this, we introduced logic into the kirbytext :slight_smile:
One last tiny question though: I would like to create sentences such as:
We are offering subpage, subpage, subpage, and subpage.
How could i inject the “and” before the last item of the subpages? usually i ask in a foreach loop for the last item and include it there. but in your example i wouldn’t know how to add exceptions…

<?php 
$urls = $page->children()->pluck('url');
$pages = $page->children()->pluck('title', ',');


function getLinks($url, $title)
{
    return '<a href="'. $url .'">'  . $title . '</a>';
}
$result = array_map('getLinks', $urls, $pages);
$links = implode(', ', $result);

// find position of  last comma
$pos = strrpos($links, ',');
//replace with ", and"
$links = substr_replace($links, ', and', $pos, strlen(','));

Thanks again.
Somehow, the replacement of the comma didn’t work in my code, so i am using this one now instead:

		$result = array_map('getLinks', $urls, $pages);
		$last = array_pop($result);
		$links = count($result) ? implode(", ", $result) . ", and " . $last : $last;
		echo Str::template($page->intro()->kt(), [
			'subpages' => $links
		]);