Skip to content

9. HTML

A web application sends HTML. In Novis you write it as an html template: HTML between backticks, with html in front.

<?nvs
// This program writes the task list as an HTML page with an `html` template.
class Task
{
public string $title;
public bool $done = false;
public function constructor(string $title)
{
$this->title = $title;
}
}
class Pages
{
public static function list(array<Task> $tasks): Core\Html\Markup
{
var $items = html``;
foreach ($tasks as Task $task) {
// `{$title}` is a hole. Its value is escaped, so `<` prints as `&lt;`.
string $title = $task->title;
$items = $items + html`<li>{$title}</li>`;
}
return html`<h1>Tasks</h1><ul>{$items}</ul>`;
}
}
array<Task> $tasks = [new Task('Buy milk'), new Task('Learn <b> tags')];
echo Pages::list($tasks), "\n";
Output
<h1>Tasks</h1><ul><li>Buy milk</li><li>Learn &lt;b&gt; tags</li></ul>

{$title} is a hole. The value of $title goes into the HTML there, and it is escaped first. The second title has <b> in it, and the page shows it as text. So a value from a user cannot add a tag or a script to your page.

{$items} is a hole too. $items has the type Core\Html\Markup, which is HTML already, so it goes in as it is. + joins two pieces of HTML.

The expression in a {$…} hole must start with a variable. For one that does not, such as a static method call, write <?= Pages::footer() ?>. It is escaped in the same way.

Read more in Syntax: string operators.

Next: step 10, a web application.