Skip to content

Core\Response::sendHtml

Sends an HTML page as the response. It also adds the header Content-Type: text/html; charset=utf-8, so the browser shows the body as a page.

The body must be a Core\Html\Markup, not a string. A Markup is HTML that is already safe to send. You get one from an html template such as `html<p>Hello</p> . You also get one from Core\Html::escape, from Core\Html::join, or with as Core\Html\Markup on a string you wrote in the program. An html template escapes every value you put in {...}. So text from a visitor is shown as text, and a <script> tag in it does not run. Core\Response::sendHtml` sends the bytes as they are and escapes nothing a second time.

The examples show a small page, text from a visitor inside a page, and a list of products built from an array.

Parameters

  • $bodyCore\Html\MarkupThe page to send. It is a Core\Html\Markup, which is HTML that is already safe. An html template, `html… , makes one and escapes the values in its {...}. Core\Html::escape also makes one. It is sent as it is and nothing is escaped again. A string` does not compile.

Return value

Returns void. Nothing. Using this and echo in one response does not compile.

Changelog

VersionChange
0.0.6The Core\Response methods that send a body now start with send: sendJson, sendHtml, sendText, sendBytes and sendSlotted. startStream starts a streamed response. The old names were json, html, text, bytes, slotted and stream.

Examples

A small page

<?nvs
// Shows how to send an HTML page as the response.
// `as Core\Html\Markup` turns HTML that you wrote in the program into a `Markup`.
Core\Html\Markup $page = "<h1>Opening hours</h1><p>Monday to Friday, 9:00 to 17:00</p>\n" as Core\Html\Markup;
// This sends the page and adds the header `Content-Type: text/html; charset=utf-8`.
Core\Response::sendHtml($page);
Output
<h1>Opening hours</h1><p>Monday to Friday, 9:00 to 17:00</p>

Text from a visitor

<?nvs
// Shows how text from a visitor is shown safely inside a page.
string $comment = "Nice shop! <script>alert('hi')</script>";
// An html template escapes every value in `{...}`. The `<` and `>` of the
// script tag become `&lt;` and `&gt;`, so the browser shows them as text.
Core\Response::sendHtml(html`<p class="comment">{$comment}</p>`);
Output
<p class="comment">Nice shop! &lt;script&gt;alert(&#39;hi&#39;)&lt;/script&gt;</p>

A list of products

<?nvs
// Shows how a shop builds a page with a list of products and sends it.
array<string> $products = ['Tea & biscuits', 'Coffee beans', 'Hot chocolate'];
// Each product becomes one `<li>` item. The `&` in the first name is escaped.
array<Core\Html\Markup> $items = [];
foreach ($products as string $product) {
$items[] = html`<li>{$product}</li>`;
}
// `Core\Html::join` puts the items together, with a line break between each two.
Core\Html\Markup $list = Core\Html::join($items, "\n" as Core\Html\Markup);
Core\Response::sendHtml(html`<h1>Our products</h1>` + ("\n<ul>\n" as Core\Html\Markup) + $list + ("\n</ul>\n" as Core\Html\Markup));
Output
<h1>Our products</h1>
<ul>
<li>Tea &amp; biscuits</li>
<li>Coffee beans</li>
<li>Hot chocolate</li>
</ul>