Core\Html::toSource
Core\Html::toSource(Core\Html\Markup $markup, string $reason): stringCore\Html::toSource( Core\Html\Markup $markup, string $reason, ): string
Gives you the HTML inside a Core\Html\Markup value as a normal string.
A Core\Html\Markup value is HTML that is safe to write into a page. You cannot cast it to a string with as string. Core\Html::toSource is the only way to get the text. The text is returned exactly as it is, so & stays &. The Markup value does not change.
The second argument is the reason: a short text that tells the next reader why the program needs the text. You write it as text in quotes, or as a const. A reason in a variable does not compile. The reason is not used for anything else. An empty reason throws a LogicError.
You need the text when HTML goes somewhere that is not the page: a cache, a database column, a file, or a JSON response.
Good to know: this does not undo Core\Html::escape. The examples show the text, the empty reason, and HTML inside a JSON response.
Parameters
$markupCore\Html\MarkupThe markup whose text you need.$reasonstringWhy this code needs the text and not the markup, written for the next person who reads it. Nothing else reads it. An empty reason is not allowed.
Return value
Returns string. The source text of the markup, as a plain string. Typical uses are caching a rendered fragment, storing it in a database column, or writing it to a file. Another use is passing it to an output that is not HTML.
Errors
LogicError$reasonis empty. Each call must say why it needs the text.
Examples
The text inside escaped html
<?nvs// Shows how to get the HTML text out of a `Core\Html\Markup` value as a normal `string`.
// `Core\Html::escape` returns a `Core\Html\Markup` value.Core\Html\Markup $name = Core\Html::escape('Tom & Jerry');
// `Core\Html::toSource` returns the same HTML as a `string`.// The second argument is the reason. It tells the next reader why you need the text.string $source = Core\Html::toSource($name, 'counting the characters of the escaped name');
// The text is not changed, so `&` stays `&`. This prints `Tom & Jerry` and then `15`.echo $source, "\n";echo Core\Str::length($source), "\n";Tom & Jerry15The reason cannot be empty
<?nvs// Shows what happens when the reason is empty.
Core\Html\Markup $bold = '<b>Sale</b>' as Core\Html\Markup;
// An empty reason is not allowed. The call throws a `LogicError` and returns nothing.try { echo Core\Html::toSource($bold, ''), "\n";} catch (LogicError $error) { echo "The reason is missing.\n";}
// With a reason, the call returns the HTML text. This prints `<b>Sale</b>`.echo Core\Html::toSource($bold, 'showing the HTML in this example'), "\n";The reason is missing.<b>Sale</b>Html for a page that updates itself
<?nvs// Shows how to send a piece of HTML inside a JSON response. A page can use it// to update one part of itself without loading again.
tainted string $product = 'Mugs & <Cups>';uint $count = 3;
// The HTML is built as a `Core\Html\Markup` value, so the product name is escaped.Core\Html\Markup $row = ('<li>' as Core\Html\Markup) + Core\Html::escape($product) + Core\Html::escape(Core\Str::format(' (%d in your cart)', $count)) + ('</li>' as Core\Html\Markup);
// A JSON value needs a `string`, so `Core\Html::toSource` returns the HTML as one.string $html = Core\Html::toSource($row, 'the page script puts this HTML into the cart list');
// This prints one JSON object. The HTML is inside it as a normal text.echo Core\Json::encode(['target' => 'cart', 'html' => $html]), "\n";{"target":"cart","html":"\u003cli\u003eMugs \u0026amp; \u0026lt;Cups\u0026gt; (3 in your cart)\u003c/li\u003e"}