Skip to content

Core\Html::join

Puts a list of HTML pieces together into one piece, with a separator between each two.

Each piece in the list is a Core\Html\Markup value, and so is the separator. Core\Html::join writes the pieces in the order of the list. The separator goes between two pieces, and never before the first piece or after the last one. An empty list gives an empty Markup.

join does not escape anything. Every piece is already Markup, so its text was escaped or written by you before. This is the same as adding the pieces with +, but a long list needs only one call.

Good to know: the list must contain Markup values. A list of plain strings does not compile, because those strings are not escaped yet. Use Core\Html::escape on each string first.

Parameters

  • $partsarray<Core\Html\Markup>The fragments to join, in list order.
  • $separatorCore\Html\MarkupThe markup written between each two parts. It is not written before the first part or after the last. With empty markup, nothing is written between parts.

Return value

Returns Core\Html\Markup. A Core\Html\Markup with every part in order. An empty list gives empty markup. Nothing is escaped, because each part and the separator are already safe markup. Escaping them again would break that markup.

Examples

A line of links

<?nvs
// Shows how to put a few HTML links in one line, with a separator between them.
array<Core\Html\Markup> $links = [
"<a href=\"/\">Home</a>" as Core\Html\Markup,
"<a href=\"/blog\">Blog</a>" as Core\Html\Markup,
"<a href=\"/about\">About</a>" as Core\Html\Markup,
];
// The separator ` | ` goes between two links, and not after the last one.
echo Core\Html::join($links, ' | ' as Core\Html\Markup), "\n";
Output
<a href="/">Home</a> | <a href="/blog">Blog</a> | <a href="/about">About</a>

An empty list

<?nvs
// Shows what `Core\Html::join` gives for a list with no pieces.
array<Core\Html\Markup> $tags = [];
// There are no pieces, so the separator is not written either. The result is an empty `Markup`.
var $joined = Core\Html::join($tags, ', ' as Core\Html\Markup);
echo '<p>Tags: ', $joined, "</p>\n";
// With one piece, the result is that piece alone.
$tags[] = '<b>news</b>' as Core\Html\Markup;
echo '<p>Tags: ', Core\Html::join($tags, ', ' as Core\Html\Markup), "</p>\n";
Output
<p>Tags: </p>
<p>Tags: <b>news</b></p>

Rows of a table

<?nvs
// Shows how to build the rows of an HTML table from a list of orders.
array<string> $customers = ['Ada & Co', 'Grace <Ltd>', "Hopper's"];
array<int> $totals = [120, 45, 300];
// Each row is built with `+`, and `+` only adds `Markup` to `Markup`.
// So the name and the total are escaped first.
array<Core\Html\Markup> $rows = [];
int $i = 0;
while ($i < 3) {
$rows[] = ('<tr><td>' as Core\Html\Markup)
+ Core\Html::escape($customers[$i])
+ ('</td><td>' as Core\Html\Markup)
+ Core\Html::escape(Core\Str::format('%d', $totals[$i]))
+ ('</td></tr>' as Core\Html\Markup);
$i = $i + 1;
}
// One call puts all rows together, with a line break between each two.
echo ("<table>\n" as Core\Html\Markup) + Core\Html::join($rows, "\n" as Core\Html\Markup) + ("\n</table>" as Core\Html\Markup), "\n";
Output
<table>
<tr><td>Ada &amp; Co</td><td>120</td></tr>
<tr><td>Grace &lt;Ltd&gt;</td><td>45</td></tr>
<tr><td>Hopper&#39;s</td><td>300</td></tr>
</table>